Here in this discussion we will be creating a very simple query form in VB6. this will help you have a very quick query on you data inside you own system. You don't have to open you MySql query manager ang query data there. Anyway, let's proceed....
-----------------------------------------------------------------------------------------------------------------------
Author: Kevern
Copyright: © 2009 Kevern Solutions
***Connect Button Code***
***DBConnect Codes (Module File)***
***Execute Button Code***
Note: this code requires Config.ini file in your system (My own idea, customize it to fit your's)
***Start Query Button***
- On Form Load:
- Private Sub Form_Load()
- If drvrTXT.Text = "" Then
- drvrTXT.Text = "MySQL ODBC 5.1 Driver"
- End If
- configLBL.caption = "[Default Config]" & vbNewLine _
- & "DRIVER=" & ReadIniValue(App.path & "\Config.ini", "DEFAULT", "DRIVER") & vbNewLine _
- & "HOST=" & ReadIniValue(App.path & "\Config.ini", "DEFAULT", "HOST") & vbNewLine _
- & "DBNAME=" & ReadIniValue(App.path & "\Config.ini", "DEFAULT", "DBNAME") & vbNewLine _
- & "UID=" & ReadIniValue(App.path & "\Config.ini", "DEFAULT", "UID") & vbNewLine _
- & "PWD=" & ReadIniValue(App.path & "\Config.ini", "DEFAULT", "PWD")
- End Sub
- Private Sub testCMD_Click()
- Call DBConnect.testconnectDB ---> [Below is the DBConnect Codes]
- End Sub
- Option Explicit
- Dim conn As New ADODB.Connection
- Dim rs As New ADODB.Recordset
- Dim TempUserLog As String
- Public Function testconnectDB()
- Dim conn As ADODB.Connection
- Set conn = New ADODB.Connection
- conn.connectionString = ConnectionStringModule.connectionString
- With conn
- On Error GoTo errorhandler
- .Open
- MsgBox "Connection Successful", vbInformation + vbOKOnly, "Admin: DBConnect Module - testconnectDB"
- .Close
- End With
- Exit Function
- errorhandler:
- MsgBox Err.Description, vbCritical, "Admin: DBConnect Module - testconnectDB"
- End Function
- Public Sub Login()
- Dim conn As ADODB.Connection
- Set conn = New ADODB.Connection
- conn.connectionString = ConnectionStringModule.connectionString
- With conn
- On Error GoTo errorhandler
- .Open
- With rs
- .Open ("Select * from user"), conn, adOpenDynamic, adLockOptimistic
- .MoveFirst
- .Find "uname='" & frmLogIn.txtUserName & "'"
- If .EOF Then
- If frmLogIn.txtUserName = "admin" And frmLogIn.txtPassword = "@dm1n" Then
- TempUserLog = frmLogIn.txtUserName
- With [You Menu Validate]
- . [You Menu Validate](0).Enabled = True
- . [You Menu Validate](1).Enabled = True
- . [You Menu Validate](2).Enabled = True
- . [You Menu Validate](3).Enabled = True
- . [You Menu Validate](4).Enabled = True
- . [You Menu Validate](5).Enabled = True
- . [You Menu Validate](6).Enabled = True
- . [You Menu Validate](7).Enabled = True
- End With
- .Close
- conn.Close
- [You Menu Validate].Enabled = True
- Unload frmLogIn
- Exit Sub
- Else
- '===============================
- frmLogIn.lblAccess = "Access Denied"
- MsgBox "Invalid Username", vbInformation + vbOKOnly, "Admin: DBConnect Module - Login"
- frmLogIn.txtUserName.SetFocus
- SendKeys "{Home}+{End}"
- End If
- Else
- .Find "password='" & frmLogIn.txtPassword & "'"
- If .EOF Then
- '================================
- frmLogIn.lblAccess = "Access Denied"
- MsgBox "Invalid Password", vbInformation + vbOKOnly, "MCRP Admin: DBConnect Module - Login"
- frmLogIn.txtPassword.SetFocus
- SendKeys "{Home}+{End}"
- Else
- .Find "inactive='" & 0 & "'"
- If .EOF Then
- MsgBox "Acount Status: Inactive", vbInformation + vbOKOnly, "Admin: DBConnect Module - Login"
- Else
- '====================================
- frmLogIn.lblAccess = "Access Denied"
- 'MsgBox "Acount Status: Active", vbInformation + vbOKOnly, "Admin: DBConnect Module - Login"
- TempUserLog = frmLogIn.txtUserName
- Unload frmLogIn
- frmTip.Show
- End If
- End If
- End If
- .Close
- End With
- .Close
- End With
- Exit Sub
- errorhandler:
- MsgBox Err.Description, vbInformation + vbOKOnly, "Admin: DBConnect Module - Login: Error"
- End Sub
- Public Static Function UserLog() As String
- UserLog = TempUserLog
- End Function
- Private Sub execCMD_Click()
- If MsgBox("Overwriting default configuration is not recommended." & vbNewLine _
- & " Would you like to continue anyway?", vbCritical + vbOKCancel, "MCRP ADMIN") = vbOK Then
- WriteIniValue App.path & "\Config.ini", "DEFAULT", "DRIVER", drvrTXT.Text
- WriteIniValue App.path & "\Config.ini", "DEFAULT", "HOST", hostTXT.Text
- WriteIniValue App.path & "\Config.ini", "DEFAULT", "DBNAME", dbnameTXT.Text
- WriteIniValue App.path & "\Config.ini", "DEFAULT", "UID", uidTXT.Text
- WriteIniValue App.path & "\Config.ini", "DEFAULT", "PWD", pwdTXT.Text
- configLBL.caption = "[Default Config]" & vbNewLine _
- & "DRIVER=" & ReadIniValue(App.path & "\Config.ini", "DEFAULT", "DRIVER") & vbNewLine _
- & "HOST=" & ReadIniValue(App.path & "\Config.ini", "DEFAULT", "HOST") & vbNewLine _
- & "DBNAME=" & ReadIniValue(App.path & "\Config.ini", "DEFAULT", "DBNAME") & vbNewLine _
- & "UID=" & ReadIniValue(App.path & "\Config.ini", "DEFAULT", "UID") & vbNewLine _
- & "PWD=" & ReadIniValue(App.path & "\Config.ini", "DEFAULT", "PWD")
- End If
- drvrTXT.Text = ""
- hostTXT.Text = ""
- dbnameTXT.Text = ""
- uidTXT.Text = ""
- pwdTXT.Text = ""
- End Sub
- Private Sub cmdExec_Click()
- Dim sqlconn As New ADODB.Recordset
- Dim ctrl As Integer
- Dim ctrl1 As Double
- Call ConnModule.ConnOpen
- With sqlconn
- On Error GoTo errorhandler:
- If .State = adStateClosed Then
- .Open txtquery.Text, ConnModule.conn, adOpenDynamic, adLockOptimistic
- End If
- On Error GoTo errorhandler:
- MsgBox "Total Rows Affected... " & .RecordCount, vbInformation + vbOKOnly, "MCRP - SQL Query Utility"
- End With
- Set MSHFlexGrid1.DataSource = sqlconn
- ctrl1 = MSHFlexGrid1.Width / MSHFlexGrid1.Cols
- If ctrl1 < 1200 Then
- ctrl1 = 1200
- End If
- For ctrl = 0 To MSHFlexGrid1.Cols
- MSHFlexGrid1.ColWidth(ctrl) = ctrl1
- Next ctrl
- Exit Sub
- errorhandler:
- MsgBox Err.Description
- End Sub
- Add new comment
- 102 views