S
stephenc
I have written a utility to work with a shared database on our network. Below
is a cut-down sample from it which reads author names from a database table.
My problem is that the database has been moved to an SQL server and I don't
know how to modify my code to access the database there.
I've already set up an SQL Server Client on my PC, but can anyone point me
in the right direction on the VBA side, please?
Regards,
Stephenc
Dim Conn As ADODB.Connection, rs As ADODB.Recordset
Dim sConnectionString As String, sSQL As String
Dim myData() As String
Dim myRecordCount As Long
'Setting up the connection...
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & "X:\docdb\Docdb.mdb"
Set Conn = New ADODB.Connection
Set rs = New ADODB.Recordset
'Reading author names...
sSQL = "SELECT [ID], [Name], From [dbo_authors] ORDER BY [ID] ASC"
Conn.Open sConnectionString
rs.Open Source:=sSQL, ActiveConnection:=Conn, _
CursorType:=adOpenKeyset, LockType:=adLockOptimistic
rs.MoveFirst
lNumRecs = rs.RecordCount
'read all records into an array
ReDim Preserve myData(lNumRecs - 1)
Do While Not rs.EOF
myData(myRecordCount) = rs.Fields(1).Value
myRecordCount = myRecordCount + 1
rs.MoveNext
Loop
rs.Close
Conn.Close
'Set the objects to nothing
Set rs = Nothing
Set Conn = Nothing
is a cut-down sample from it which reads author names from a database table.
My problem is that the database has been moved to an SQL server and I don't
know how to modify my code to access the database there.
I've already set up an SQL Server Client on my PC, but can anyone point me
in the right direction on the VBA side, please?
Regards,
Stephenc
Dim Conn As ADODB.Connection, rs As ADODB.Recordset
Dim sConnectionString As String, sSQL As String
Dim myData() As String
Dim myRecordCount As Long
'Setting up the connection...
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & "X:\docdb\Docdb.mdb"
Set Conn = New ADODB.Connection
Set rs = New ADODB.Recordset
'Reading author names...
sSQL = "SELECT [ID], [Name], From [dbo_authors] ORDER BY [ID] ASC"
Conn.Open sConnectionString
rs.Open Source:=sSQL, ActiveConnection:=Conn, _
CursorType:=adOpenKeyset, LockType:=adLockOptimistic
rs.MoveFirst
lNumRecs = rs.RecordCount
'read all records into an array
ReDim Preserve myData(lNumRecs - 1)
Do While Not rs.EOF
myData(myRecordCount) = rs.Fields(1).Value
myRecordCount = myRecordCount + 1
rs.MoveNext
Loop
rs.Close
Conn.Close
'Set the objects to nothing
Set rs = Nothing
Set Conn = Nothing