Hello experts..
I'm using ADO,VB6 and MS Access as database. I would like to create a form as template for many subjects. I need to make dynamic connection based on subject. I created a module for ADO connection then used IF statement but fail. Here is my code in the module(.bas):
Here is my code in form_load:
Hope someone can help. The error says "Object variable or With block variable not set".
I'm using ADO,VB6 and MS Access as database. I would like to create a form as template for many subjects. I need to make dynamic connection based on subject. I created a module for ADO connection then used IF statement but fail. Here is my code in the module(.bas):
Code:
Option Explicit
Public snconn As ADODB.Connection
Public sncmd As ADODB.Command
Public rs As ADODB.Recordset
Public Sub connectdb()
Set snconn = New ADODB.Connection
snconn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" _
& GetAppPath _
& "KSSR.mdb"
Set sncmd = New ADODB.Command
Set sncmd.ActiveConnection = snconn
sncmd.CommandType = adCmdText
End Sub
Public Sub DisconnectDB()
snconn.Close
Set sncmd = Nothing
Set snconn = Nothing
End Sub
Public Function GetAppPath() As String
'-----------------------------------------------------------------------------
Dim strAppPath As String
strAppPath = App.Path
If Right$(strAppPath, 1) <> "\" Then
strAppPath = strAppPath & "\"
End If
GetAppPath = strAppPath
End Function
Public Sub sqlSubject()
If frmPBS.tabSn2.Visible = True Then
Set rs = New ADODB.Recordset
With rs
.Source = "Select * from sn2"
.ActiveConnection = snconn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.Open
.Sort = "NAMA ASC"
End With
End If
If frmPBS.tabMath2.Visible = True Then
Set rs = New ADODB.Recordset
With rs
.Source = "Select * from math2"
.ActiveConnection = snconn
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.Open
.Sort = "NAMA ASC"
End With
End If
End Sub
Code:
connectdb sqlSubject If rs.RecordCount > 1 Then rs.MoveNext end if
Comment