How to connect ADO dynamically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mdtam
    New Member
    • Jan 2013
    • 22

    #1

    How to connect ADO dynamically

    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):
    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
    Here is my code in form_load:
    Code:
    connectdb
    sqlSubject
    If rs.RecordCount > 1 Then
    rs.MoveNext
    end if
    Hope someone can help. The error says "Object variable or With block variable not set".
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    It would help to know which line is causing the error.

    Comment

    • mdtam
      New Member
      • Jan 2013
      • 22

      #3
      the yellow line shows on "rs.recordcount >1".

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        That means that your rs varible never got set. Which most likely means that frmPBS.tabSn2.V isible and If frmPBS.tabMath2 .Visible are both false.

        Comment

        Working...