• We just launched and are currently in beta. Join us as we build and grow the community.

How to create query form in vb6?

johnwillard45

System Load Balancer
J Rep
0
0
0
Rep
0
J Vouches
0
0
0
Vouches
0
Posts
55
Likes
101
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Submitted by
kevern010101

on
Fri, 07/30/2010 - 16:13

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
  1. On

    Form Load:
  2. Private

    Sub

    Form_Load()
  3. If

    drvrTXT.Text = ""

    Then
  4. drvrTXT.Text = "MySQL ODBC 5.1 Driver"
  5. End

    If
  6. configLBL.caption = "[Default Config]"

    & vbNewLine _
  7. & "DRIVER="

    & ReadIniValue(App.path & "\Config.ini"

    , "DEFAULT"

    , "DRIVER"

    ) & vbNewLine _
  8. & "HOST="

    & ReadIniValue(App.path & "\Config.ini"

    , "DEFAULT"

    , "HOST"

    ) & vbNewLine _
  9. & "DBNAME="

    & ReadIniValue(App.path & "\Config.ini"

    , "DEFAULT"

    , "DBNAME"

    ) & vbNewLine _
  10. & "UID="

    & ReadIniValue(App.path & "\Config.ini"

    , "DEFAULT"

    , "UID"

    ) & vbNewLine _
  11. & "PWD="

    & ReadIniValue(App.path & "\Config.ini"

    , "DEFAULT"

    , "PWD"

    )

  12. End

    Sub

***Connect Button Code***
  1. Private

    Sub

    testCMD_Click()
  2. Call

    DBConnect.testconnectDB ---> [Below is

    the DBConnect Codes]
  3. End

    Sub

***DBConnect Codes (Module File)***
  1. Option

    Explicit
  2. Dim

    conn As

    New

    ADODB.Connection
  3. Dim

    rs As

    New

    ADODB.Recordset
  4. Dim

    TempUserLog As

    String

  5. Public

    Function

    testconnectDB()
  6. Dim

    conn As

    ADODB.Connection
  7. Set

    conn = New

    ADODB.Connection
  8. conn.connectionString = ConnectionStringModule.connectionString
  9. With

    conn
  10. On

    Error

    GoTo

    errorhandler
  11. .Open
  12. MsgBox "Connection Successful"

    , vbInformation + vbOKOnly, "Admin: DBConnect Module - testconnectDB"
  13. .Close
  14. End

    With
  15. Exit

    Function
  16. errorhandler

    :
  17. MsgBox Err.Description, vbCritical, "Admin: DBConnect Module - testconnectDB"
  18. End

    Function
  19. Public

    Sub

    Login()
  20. Dim

    conn As

    ADODB.Connection
  21. Set

    conn = New

    ADODB.Connection

  22. conn.connectionString = ConnectionStringModule.connectionString

  23. With

    conn
  24. On

    Error

    GoTo

    errorhandler
  25. .Open
  26. With

    rs
  27. .Open

    ("Select * from user"

    ), conn, adOpenDynamic, adLockOptimistic
  28. .MoveFirst
  29. .Find "uname='"

    & frmLogIn.txtUserName & "'"
  30. If

    .EOF Then
  31. If

    frmLogIn.txtUserName = "admin"

    And

    frmLogIn.txtPassword = "@dm1n"

    Then
  32. TempUserLog = frmLogIn.txtUserName
  33. With

    [You Menu Validate]
  34. . [You Menu Validate](0).Enabled = True
  35. . [You Menu Validate](1).Enabled = True
  36. . [You Menu Validate](2).Enabled = True
  37. . [You Menu Validate](3).Enabled = True
  38. . [You Menu Validate](4).Enabled = True
  39. . [You Menu Validate](5).Enabled = True
  40. . [You Menu Validate](6).Enabled = True
  41. . [You Menu Validate](7).Enabled = True
  42. End

    With
  43. .Close
  44. conn.Close
  45. [You Menu Validate].Enabled = True
  46. Unload frmLogIn

  47. Exit

    Sub
  48. Else
  49. '===============================
  50. frmLogIn.lblAccess = "Access Denied"
  51. MsgBox "Invalid Username"

    , vbInformation + vbOKOnly, "Admin: DBConnect Module - Login"
  52. frmLogIn.txtUserName.SetFocus
  53. SendKeys "{Home}+{End}"

  54. End

    If
  55. Else
  56. .Find "password='"

    & frmLogIn.txtPassword & "'"
  57. If

    .EOF Then
  58. '================================
  59. frmLogIn.lblAccess = "Access Denied"
  60. MsgBox "Invalid Password"

    , vbInformation + vbOKOnly, "MCRP Admin: DBConnect Module - Login"
  61. frmLogIn.txtPassword.SetFocus
  62. SendKeys "{Home}+{End}"
  63. Else
  64. .Find "inactive='"

    & 0 & "'"
  65. If

    .EOF Then
  66. MsgBox "Acount Status: Inactive"

    , vbInformation + vbOKOnly, "Admin: DBConnect Module - Login"
  67. Else
  68. '====================================
  69. frmLogIn.lblAccess = "Access Denied"
  70. 'MsgBox "Acount Status: Active", vbInformation + vbOKOnly, "Admin: DBConnect Module - Login"
  71. TempUserLog = frmLogIn.txtUserName
  72. Unload frmLogIn
  73. frmTip.Show
  74. End

    If
  75. End

    If
  76. End

    If
  77. .Close
  78. End

    With
  79. .Close
  80. End

    With
  81. Exit

    Sub
  82. errorhandler

    :
  83. MsgBox Err.Description, vbInformation + vbOKOnly, "Admin: DBConnect Module - Login: Error"
  84. End

    Sub
  85. Public

    Static

    Function

    UserLog() As

    String
  86. UserLog = TempUserLog
  87. End

    Function

***Execute Button Code***

Note: this code requires Config.ini file in your system (My own idea, customize it to fit your's)
  1. Private

    Sub

    execCMD_Click()
  2. If

    MsgBox("Overwriting default configuration is not recommended."

    & vbNewLine _
  3. & " Would you like to continue anyway?"

    , vbCritical + vbOKCancel, "MCRP ADMIN"

    ) = vbOK Then
  4. WriteIniValue App.path & "\Config.ini"

    , "DEFAULT"

    , "DRIVER"

    , drvrTXT.Text
  5. WriteIniValue App.path & "\Config.ini"

    , "DEFAULT"

    , "HOST"

    , hostTXT.Text
  6. WriteIniValue App.path & "\Config.ini"

    , "DEFAULT"

    , "DBNAME"

    , dbnameTXT.Text
  7. WriteIniValue App.path & "\Config.ini"

    , "DEFAULT"

    , "UID"

    , uidTXT.Text
  8. WriteIniValue App.path & "\Config.ini"

    , "DEFAULT"

    , "PWD"

    , pwdTXT.Text

  9. configLBL.caption = "[Default Config]"

    & vbNewLine _
  10. & "DRIVER="

    & ReadIniValue(App.path & "\Config.ini"

    , "DEFAULT"

    , "DRIVER"

    ) & vbNewLine _
  11. & "HOST="

    & ReadIniValue(App.path & "\Config.ini"

    , "DEFAULT"

    , "HOST"

    ) & vbNewLine _
  12. & "DBNAME="

    & ReadIniValue(App.path & "\Config.ini"

    , "DEFAULT"

    , "DBNAME"

    ) & vbNewLine _
  13. & "UID="

    & ReadIniValue(App.path & "\Config.ini"

    , "DEFAULT"

    , "UID"

    ) & vbNewLine _
  14. & "PWD="

    & ReadIniValue(App.path & "\Config.ini"

    , "DEFAULT"

    , "PWD"

    )
  15. End

    If

  16. drvrTXT.Text = ""
  17. hostTXT.Text = ""
  18. dbnameTXT.Text = ""
  19. uidTXT.Text = ""
  20. pwdTXT.Text = ""

  21. End

    Sub

***Start Query Button***
  1. Private

    Sub

    cmdExec_Click()
  2. Dim

    sqlconn As

    New

    ADODB.Recordset
  3. Dim

    ctrl As

    Integer
  4. Dim

    ctrl1 As

    Double
  5. Call

    ConnModule.ConnOpen
  6. With

    sqlconn
  7. On

    Error

    GoTo

    errorhandler

    :
  8. If

    .State = adStateClosed Then
  9. .Open

    txtquery.Text, ConnModule.conn, adOpenDynamic, adLockOptimistic
  10. End

    If
  11. On

    Error

    GoTo

    errorhandler

    :
  12. MsgBox "Total Rows Affected... "

    & .RecordCount, vbInformation + vbOKOnly, "MCRP - SQL Query Utility"
  13. End

    With
  14. Set

    MSHFlexGrid1.DataSource = sqlconn

  15. ctrl1 = MSHFlexGrid1.Width / MSHFlexGrid1.Cols
  16. If

    ctrl1 < 1200 Then
  17. ctrl1 = 1200
  18. End

    If
  19. For

    ctrl = 0 To

    MSHFlexGrid1.Cols
  20. MSHFlexGrid1.ColWidth(ctrl) = ctrl1
  21. Next

    ctrl

  22. Exit

    Sub
  23. errorhandler

    :
  24. MsgBox Err.Description
  25. End

    Sub

 

438,139

315,428

315,437

Top