Hello,
I'm trying to create a central function that runs a connection to an SQL
Server database. The connection etc works, but when I try to call it, I get
an error saying "Runtime-Error 91: Object variable or with block variable
not set". I have pasted the code below. The first section is the function
that I'm using, the second function is only the test that calls teh
genErrorColl function
'************** *** Start Code
*************** *************** *************** **
Private Const strServerAddres s = "MyDbServer "
Private Const strDatabaseName = "MyDatabase "
Private Const strUserName = "MyUserName "
Private Const strPassword = "MyPassword "
Public Function genErrorColl(st rTableName As String) As ADODB.Recordset
Dim cnn1 As ADODB.connectio n
Dim strCnn As String
Dim varDate As Variant
' Open connection.
strCnn = "Provider=SQLOL EDB.1;Persist Security Info=False;User ID=" &
strUserName & ";password= " & strPassword & ";Initial Catalog=" &
strDatabaseName & ";Data Source=" & strServerAddres s & ";Use Procedure for
Prepare=1;Auto Translate=True; Packet Size=4096;Use Encryption for
Data=False;Tag with column collation when possible=False"
Set cnn1 = New ADODB.connectio n
cnn1.Open strCnn
' Open the Requested Table or View
Set OpenRST = New ADODB.Recordset
OpenRST.CursorT ype = adOpenKeyset
OpenRST.LockTyp e = adLockOptimisti c
OpenRST.Open strTableName, cnn1, , , adCmdTable
cnn1.Close
End Function
Private Sub testConn()
Dim rst As ADODB.Recordset
Set rst = genErrorColl("v iewErrorCollect ion")
Do While Not rst.EOF
Debug.Print rst!kErrorLogID
rst.MoveNext
Loop
End Sub
'************* End of Code *************** *************** ******
Any help would be great.
Thanks!
I'm trying to create a central function that runs a connection to an SQL
Server database. The connection etc works, but when I try to call it, I get
an error saying "Runtime-Error 91: Object variable or with block variable
not set". I have pasted the code below. The first section is the function
that I'm using, the second function is only the test that calls teh
genErrorColl function
'************** *** Start Code
*************** *************** *************** **
Private Const strServerAddres s = "MyDbServer "
Private Const strDatabaseName = "MyDatabase "
Private Const strUserName = "MyUserName "
Private Const strPassword = "MyPassword "
Public Function genErrorColl(st rTableName As String) As ADODB.Recordset
Dim cnn1 As ADODB.connectio n
Dim strCnn As String
Dim varDate As Variant
' Open connection.
strCnn = "Provider=SQLOL EDB.1;Persist Security Info=False;User ID=" &
strUserName & ";password= " & strPassword & ";Initial Catalog=" &
strDatabaseName & ";Data Source=" & strServerAddres s & ";Use Procedure for
Prepare=1;Auto Translate=True; Packet Size=4096;Use Encryption for
Data=False;Tag with column collation when possible=False"
Set cnn1 = New ADODB.connectio n
cnn1.Open strCnn
' Open the Requested Table or View
Set OpenRST = New ADODB.Recordset
OpenRST.CursorT ype = adOpenKeyset
OpenRST.LockTyp e = adLockOptimisti c
OpenRST.Open strTableName, cnn1, , , adCmdTable
cnn1.Close
End Function
Private Sub testConn()
Dim rst As ADODB.Recordset
Set rst = genErrorColl("v iewErrorCollect ion")
Do While Not rst.EOF
Debug.Print rst!kErrorLogID
rst.MoveNext
Loop
End Sub
'************* End of Code *************** *************** ******
Any help would be great.
Thanks!
Comment