Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Everytime I press the command button , "error 424 Object required" is showing up


VB
Public con1 As New ADODB.Connection
Public rs As New ADODB.Recordset
Dim q1 As String
Dim i As Integer
Public Sub con()
    con1.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = E:\TrainTimeSchedule.accdb"
    Set con1 = New ADODB.Connection
    con1.Open ("dsn123")
End Sub
Public Sub opentable(sql As String)
    Set rs = con1.Execute(sql)
End Sub

Private Sub Command1_Click()
    
    q1 = "Select TrainID,TrainName,Source,Destination,Stations,DayOfWeek from TrainTable Where Source ='" & tex1.Text & "'" & "and Destination = '" & Text2.Text & "')"
    opentable (q1)
    MSFlexGrid1.DataSource = rs
End Sub
Posted
Comments
[no name] 21-Apr-13 5:48am    
Probably because you are never calling con() anywhere.
StianSandberg 23-Apr-13 8:48am    
your code is vulnerable for sql-injections!

1 solution

Hi Please use follow of code for solve your problem:

VB
Public adodbcon As New ADODB.Connection
Public adodbrs As New ADODB.Recordset


Public Sub opentable(sql As String)
    adodbcon .ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = E:\TrainTimeSchedule.accdb"
    Set adodbcon = New ADODB.Connection
    adodbcon.Open ("dsn123")
    Set adodbrs = adodbcon .Execute(sql)
End Sub

 
Private Sub Command1_Click()
Dim q1 as String    
    q1 = "Select TrainID,TrainName,Source,Destination,Stations,DayOfWeek from TrainTable Where Source =N'" & tex1.Text & "'" & "and Destination =N'" & Text2.Text & "')"
    opentable(q1)
    MSFlexGrid1.DataSource = rs
End Sub


Remember finally your should call adodbcon.Close() for closing adodb connection because adodb is connection base and if you don`t do it you will get error at the next using as adodb.Open().
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900