Need help with transferring ADODB codes to module
Hi guys! So after a decade my boss wants me to create a simple visual basic program to track our document routing within the department. I'm having a hard time :(
So i have these codes:
Code:
Dim oRS As ADODB.Recordset
Dim sSQL As String
Dim i As Integer
Dim oConn As ADODB.Connection
Set oConn = New ADODB.Connection
oConn.Provider = "Microsoft.Jet.OLEDB.4.0"
oConn.ConnectionString = "Data Source=" & App.Path & "\tracking.mdb"
oConn.Open
sSQL = "SELECT username, password, fname FROM users WHERE username = '" & txtusername.Text & "'"
Set oRS = New Recordset
'Open our recordset
oRS.Open sSQL, oConn
If oRS.EOF Then
MsgBox "Invalid Username!", vbInformation, "Try Again"
Else
If oRS.Fields.Item("password") <> txtpassword.Text Then
MsgBox "Invalid Password!", vbInformation, "Try Again"
txtpassword.SetFocus
Else
LoginSucceeded = True
logged_user = oRS.Fields("fname").Value
Me.Hide
End If
End If
oRS.Close
oConn.Close
this works fine but everytime i create another form like add new user, i have to copy everything. Can someone tell me which codes should i transfer to "new module"? and which codes should i call in every form i make? Thanks
Re: Need help with transferring ADODB codes to module
That looks like code for a log in form. There should be no reason to have more than one in a project so no need to have multiple copies of that code.
Yes it could be placed in a module but it would have to be changed quite a bit, more if you want to do it right but there would be no point.
On another note, it is not called codes. It is code. Even if a project has 1 million lines of code it is still called code. Codes would be valid only if you were talking about multiple different projects and even then would be a bit odd to use that term.
Re: Need help with transferring ADODB codes to module
sorry if my question was too vague. I highlighted the code i was referring to.
the sample code i posted above is for my login page. i was asking IF i have "add new user account" page, i need to copy paste that highlighted code to connect to my access database. So i was thinking if maybe i could create a new module and declare that highlighted code as global
Re: Need help with transferring ADODB codes to module
Well from that code the only thing I would consider placing in a module is the declaration for the connection and maybe the recordset.
Code:
Public oConn As ADODB.Connection
Public oRS As ADODB.Recordset
In the load event of the first form
Code:
Set oConn = New ADODB.Connection
oConn.Provider = "Microsoft.Jet.OLEDB.4.0"
oConn.ConnectionString = "Data Source=" & App.Path & "\tracking.mdb"
Set oRS = New ADODB.Recordset
Then you could open and close the connection and recordset as needed at other places in your code.