Results 1 to 27 of 27

Thread: SOLVED MqSQL With VB front end, entering info

  1. #1

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Resolved SOLVED MqSQL With VB front end, entering info

    Hello Everyone.
    I have a simple table with First_Name Last_Name and Age as colums, in the table JoeFox.

    I was wondering how to create a way to have the user type in the first name, last name, and age into fields on the VB front end program, hit update, and it gets inserted into the database?

    I am able to query the database, but i need help inserting etc.

    I am using MySQL with a VB Front end.

    Thanks for the help!
    Joe
    Last edited by joefox; Jun 7th, 2005 at 04:01 PM.

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: MqSQL With VB front end, entering info

    How do you connect to the database?

    Here is a function I have used in one of my old programs, it might help :

    VB Code:
    1. Function AddUser(sUsername As String, sPassword As String, sEMail As String) As Boolean
    2.     On Error GoTo ErrAdd
    3.    
    4.     AddUser = True
    5.    
    6.     If Rs.RecordCount <> 0 Then
    7.         If UserExists(sUsername) Then
    8.             AddUser = False
    9.             StringError = "ERROR : Username already exists."
    10.             Exit Function
    11.         End If
    12.     End If
    13.    
    14.     With Rs
    15.         .AddNew 'adding new record
    16.         .Fields("Username") = sUsername
    17.         .Fields("Password") = sPassword
    18.         .Fields("DateRegistered") = Format$(Date, "dd/mm/yyyy")
    19.         If Len(Trim$(sEMail)) Then
    20.             .Fields("EMail") = sEMail
    21.         End If
    22.         .Update 'this updates the recordset etc.
    23.     End With
    24.     If Rs.State <> adStateClosed Then
    25.         On Error Resume Next
    26.         Rs.Close
    27.     End If
    28.  
    29. Exit Function
    30.  
    31. ErrAdd:
    32.     AddUser = False
    33.     StringError = "ERROR : " & Err.Number & vbNewLine & Err.Description & vbNewLine
    34. End Function


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: MqSQL With VB front end, entering info

    I am connection using:
    VB Code:
    1. Private Sub Form_Load()
    2. ' Connection Strings
    3. ' Visual Basic Program Connection to Sequel Server
    4. Set oConn = New ADODB.Connection
    5. With oConn
    6.     .CursorLocation = adUseClient
    7.     .Open "Provider=SQLOLEDB.1;Server=MANIFEST2;Database=Northwind;UID=sa;PWD=;"
    8.  
    9. End With
    10. Label2.Caption = "Connected..."
    11. End Sub

    So just using the ".update" will updated it to the MYSQL databse? I thought the .update was for access?

  4. #4
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: MqSQL With VB front end, entering info

    Quote Originally Posted by joefox
    I am connection using:
    VB Code:
    1. Private Sub Form_Load()
    2. ' Connection Strings
    3. ' Visual Basic Program Connection to Sequel Server
    4. Set oConn = New ADODB.Connection
    5. With oConn
    6.     .CursorLocation = adUseClient
    7.     .Open "Provider=SQLOLEDB.1;Server=MANIFEST2;Database=Northwind;UID=sa;PWD=;"
    8.  
    9. End With
    10. Label2.Caption = "Connected..."
    11. End Sub

    So just using the ".update" will updated it to the MYSQL databse? I thought the .update was for access?
    Now I'm confused. Do you mean MS SQL og MySQL. The connection string above is for MS SQL.

  5. #5

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: MqSQL With VB front end, entering info

    Your right, MSSQL sorry!

    I just wanted to know how to make a basic function where i can add a persons first name, last name, and age into the database.

    I know how to do that in vb to access, but i dont know how to do that in vb to MYSQL

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MqSQL With VB front end, entering info

    Your right, MSSQL sorry!
    ..vb to MYSQL


    The method that manavo11 posted (except the "If UserExists" part) should work fine for any updateable recordset that has been created using ADO or DAO.

  7. #7

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: MqSQL With VB front end, entering info

    Ok this is what i have, but how does the program know what fields to grab from on my vb front end?

    First Name is TextBox1
    Last Name is TextBox2
    Age is TextBox3

    In my database, they are called First_Name Last_Name and Age

    Here is what i have, but i dont know if im no the right track:

    VB Code:
    1. Public Sub Command1_Click()
    2. AddUser
    3. End Sub
    4.  
    5. Function AddUser(SFirstName As String, sLastName As String, SAge As String) As Boolean
    6.     On Error GoTo ErrAdd
    7.     AddUser = True
    8.     With Rs
    9.         .AddNew 'adding new record
    10.         .Fields("First_Name") = sUsername
    11.         .Fields("Last_Name") = sPassword
    12.         .Fields("Age") = SAge
    13.         .Update 'this updates the recordset etc.
    14.     End With
    15.     If Rs.State <> adStateClosed Then
    16.         On Error Resume Next
    17.         Rs.Close
    18.     End If
    19.  
    20. Exit Function
    21.  
    22. ErrAdd:
    23.     AddUser = False
    24.     StringError = "ERROR : " & Err.Number & vbNewLine & Err.Description & vbNewLine
    25. End Function

  8. #8
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MqSQL With VB front end, entering info

    That looks ok to me, the only change you need to make is the call in Command1_Click, it should be something like this:
    VB Code:
    1. Call AddUser(TextBox1.Text, TextBox2.Text, TextBox3.Text)
    By the way, you should probably have Age as an numeric (probably integer) rather than a string, and have validation in Command1_Click like this:
    VB Code:
    1. If IsNumeric(TextBox3.Text) And (TextBox3.Text <> "") Then
    2.   Call AddUser(TextBox1.Text, TextBox2.Text, CInt(TextBox3.Text))
    3. Else
    4.   MsgBox "Please enter a valid age!"
    5. End If

  9. #9

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: MqSQL With VB front end, entering info

    Ok this is what i have, but it says "Object Required"

    VB Code:
    1. Public Sub Command1_Click()
    2. Call AddUser(TextBox1.Text, TextBox2.Text, TextBox3.Text) ' gives error here
    3. End Sub
    4.  
    5. Function AddUser(SFirstName As String, sLastName As String, SAge As String) As Boolean
    6.     On Error GoTo ErrAdd
    7.     AddUser = True
    8.     With Rs
    9.         .AddNew 'adding new record
    10.         .Fields("First_Name") = sUsername
    11.         .Fields("Last_Name") = sPassword
    12.         .Fields("Age") = SAge
    13.         .Update 'this updates the recordset etc.
    14.     End With
    15.     If Rs.State <> adStateClosed Then
    16.         On Error Resume Next
    17.         Rs.Close
    18.     End If
    19.  
    20. Exit Function
    21.  
    22. ErrAdd:
    23.     AddUser = False
    24.     StringError = "ERROR : " & Err.Number & vbNewLine & Err.Description & vbNewLine
    25. End Function

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MqSQL With VB front end, entering info

    From what you said I assumed that TextBox1/2/3 were the names of textboxes that held the data, it seems that one or more of them isn't.

  11. #11

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: MqSQL With VB front end, entering info

    Ok, i changed back my textbox's on my vb app so they match those in the code.

    However, when i click the button nothing happens:

    VB Code:
    1. Public Sub Command1_Click()
    2. Call AddUser(TextBox1.Text, TextBox2.Text, TextBox3.Text)
    3. End Sub
    4.  
    5. Function AddUser(SFirstName As String, sLastName As String, SAge As String) As Boolean
    6.     On Error GoTo ErrAdd
    7.     AddUser = True
    8.     With Rs
    9.         .AddNew 'adding new record
    10.         .Fields("First_Name") = SFirstName
    11.         .Fields("Last_Name") = sLastName
    12.         .Fields("Age") = SAge
    13.         .Update 'this updates the recordset etc.
    14.     End With
    15.     If Rs.State <> adStateClosed Then
    16.         On Error Resume Next
    17.         Rs.Close
    18.     End If
    19.  
    20. Exit Function
    21.  
    22. ErrAdd:
    23.     AddUser = False
    24.     StringError = "ERROR : " & Err.Number & vbNewLine & Err.Description & vbNewLine
    25. End Function

  12. #12
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MqSQL With VB front end, entering info

    I presume you are getting an error of some sort, the example code effectively hides the error as it is just stored in the string StringError.

    I would recommend changing 'StringError =' to 'Msgbox' , so that you can see what the problem is, and hopefully fix it.

    I would assume that there is a problem with the recordset. it may not be in scope, it may not be updateable, or you may need to change Rs to the name of your recordset.

  13. #13

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: MqSQL With VB front end, entering info

    Ok i just took out the Error part, and wanted to see what it says: it gives me an
    Object Required Error

    VB Code:
    1. Public Sub Command1_Click()
    2. Call AddUser(TextBox1.Text, TextBox2.Text, TextBox3.Text)
    3. End Sub
    4.  
    5. Function AddUser(sFirstName As String, sLastName As String, SAge As String) As Boolean
    6.     'On Error GoTo ErrAdd
    7.     AddUser = True
    8.     With Rs
    9.         .AddNew 'adding new record  GIVES ME ERROR HERE
    10.         .Fields("First_Name") = sFirstName
    11.         .Fields("Last_Name") = sLastName
    12.         .Fields("Age") = SAge
    13.         .Update 'this updates the recordset etc.
    14.     End With
    15.     If Rs.State <> adStateClosed Then
    16.         On Error Resume Next
    17.         Rs.Close
    18.     End If
    19.    
    20. Label2.Caption = "Recordset Updated..."
    21.  
    22. Exit Function
    23. Label2.Caption = "Recordset Updated 2..."
    24.  
    25. End Function

  14. #14
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MqSQL With VB front end, entering info

    Rs isn't set. Is that the name of your recordset? Is it in scope?

  15. #15

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: MqSQL With VB front end, entering info

    It should be in my scope, i set rst above:

    It gives me same error in same place, here is my entire code:

    VB Code:
    1. Dim oConn As ADODB.Connection
    2. Dim Server As String
    3. Dim Database As String
    4.  
    5.  
    6.  
    7. Private Sub Form_Load()
    8. ' Connection Strings
    9. ' Visual Basic Program Connection to Sequel Server
    10. Set oConn = New ADODB.Connection
    11. With oConn
    12.     .CursorLocation = adUseClient
    13.     .Open "Provider=SQLOLEDB.1;Server=MANIFEST2;Database=Northwind;UID=sa;PWD=;"
    14.  
    15. End With
    16. Label2.Caption = "Connected..."
    17. End Sub
    18.  
    19.  
    20. ' Button that Runs the Actual Query
    21. Private Sub Search_Click()
    22.   Dim rst As ADODB.Recordset
    23.     Dim strSql As String, strResults As String
    24.     Dim lngMaxRcs As Long, lngRcs As Long
    25.  
    26. '---- create the sql statement and print it to the immediates window
    27.     strSql = "SELECT [First_Name] + '  '  +  [Last_Name] as FullName,  [Age] FROM [JoeFox] WHERE [First_Name] like '%" & txtSearch.Text & "%'"
    28.     Debug.Print strSql
    29.    
    30. '---- Open the recordset
    31.     Set rst = New ADODB.Recordset
    32.     rst.Open strSql, oConn, 3, 3, 1 'static,optimistic,adCmdText
    33.    
    34.     If rst.EOF Then
    35.         'If you want a message box to come up, then uncomment the code below
    36.         'MsgBox "No records returned", vbOKOnly + vbInformation, "Search"
    37.         txtResults.Text = "No Records Match the Criteria." & vbCrLf & "Please try again."
    38.     Else
    39.         rst.MoveLast
    40.         rst.MoveFirst
    41.         lngMaxRcs = rst.RecordCount
    42.         For lngRcs = 1 To lngMaxRcs
    43.             strResults = strResults & IIf(Len(strResults) > 0, vbCrLf, "") & rst("fullname") & " Age=" & rst("age")
    44.             rst.MoveNext
    45.         Next
    46.         ' This is where the results are displayed
    47.         txtResults.Text = strResults
    48.         Label2.Caption = "Search Completed"
    49.     End If
    50.     ' Closes the Record set so that it dosent us up memory space
    51.     rst.Close
    52.    
    53.     Set rst = Nothing
    54. End Sub
    55.  
    56. Public Sub Command1_Click()
    57. Call AddUser(TextBox1.Text, TextBox2.Text, TextBox3.Text)
    58. End Sub
    59.  
    60. Function AddUser(sFirstName As String, sLastName As String, SAge As String) As Boolean
    61.     'On Error GoTo ErrAdd
    62.     AddUser = True
    63.     With rst
    64.         .AddNew 'adding new record
    65.         .Fields("First_Name") = sFirstName
    66.         .Fields("Last_Name") = sLastName
    67.         .Fields("Age") = SAge
    68.         .Update 'this updates the recordset etc.
    69.     End With
    70.     If rst.State <> adStateClosed Then
    71.         On Error Resume Next
    72.         rst.Close
    73.     End If
    74.    
    75. Label2.Caption = "Recordset Updated..."
    76.  
    77. Exit Function
    78. Label2.Caption = "Recordset Updated 2..."
    79.  
    80. End Function

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MqSQL With VB front end, entering info

    It isn't in scope I'm afraid, it is currently limited to the sub Search_Click. To fix this, move the declaration line to General Declarations (the same place as the declaration of oConn etc.), and move the following two lines from Search_Click to Form_Unload:
    VB Code:
    1. rst.Close
    2.     Set rst = Nothing
    You should also add these lines to Form_Unload (after the two lines above):
    VB Code:
    1. oConn.Close
    2.     Set oConn = Nothing

    You should probably also remove the following section from AddUser, as it closes the recordset after adding a new record.
    VB Code:
    1. If rst.State <> adStateClosed Then
    2.         On Error Resume Next
    3.         rst.Close
    4.     End If

  17. #17

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: MqSQL With VB front end, entering info

    Ok i tried what you said above and it still gives me the object required error

    VB Code:
    1. Dim oConn As ADODB.Connection
    2. Dim Server As String
    3. Dim Database As String
    4.  
    5.  
    6.  
    7. Private Sub Form_Load()
    8. ' Connection Strings
    9. ' Visual Basic Program Connection to Sequel Server
    10. Set oConn = New ADODB.Connection
    11. With oConn
    12.     .CursorLocation = adUseClient
    13.     .Open "Provider=SQLOLEDB.1;Server=MANIFEST2;Database=Northwind;UID=sa;PWD=;"
    14.  
    15. End With
    16. Label2.Caption = "Connected..."
    17. End Sub
    18.  
    19.  
    20. Private Sub Form_Unload(Cancel As Integer)
    21.     rst.Close
    22.     Set rst = Nothing
    23.     oConn.Close
    24.     Set oConn = Nothing
    25. End Sub
    26.  
    27. ' Button that Runs the Actual Query
    28. Private Sub Search_Click()
    29.   Dim rst As ADODB.Recordset
    30.     Dim strSql As String, strResults As String
    31.     Dim lngMaxRcs As Long, lngRcs As Long
    32.  
    33. '---- create the sql statement and print it to the immediates window
    34.     strSql = "SELECT [First_Name] + '  '  +  [Last_Name] as FullName,  [Age] FROM [JoeFox] WHERE [First_Name] like '%" & txtSearch.Text & "%'"
    35.     Debug.Print strSql
    36.    
    37. '---- Open the recordset
    38.     Set rst = New ADODB.Recordset
    39.     rst.Open strSql, oConn, 3, 3, 1 'static,optimistic,adCmdText
    40.    
    41.     If rst.EOF Then
    42.         'If you want a message box to come up, then uncomment the code below
    43.         'MsgBox "No records returned", vbOKOnly + vbInformation, "Search"
    44.         txtResults.Text = "No Records Match the Criteria." & vbCrLf & "Please try again."
    45.     Else
    46.         rst.MoveLast
    47.         rst.MoveFirst
    48.         lngMaxRcs = rst.RecordCount
    49.         For lngRcs = 1 To lngMaxRcs
    50.             strResults = strResults & IIf(Len(strResults) > 0, vbCrLf, "") & rst("fullname") & " Age=" & rst("age")
    51.             rst.MoveNext
    52.         Next
    53.         ' This is where the results are displayed
    54.         txtResults.Text = strResults
    55.         Label2.Caption = "Search Completed"
    56.     End If
    57.     ' Closes the Record set so that it dosent us up memory space
    58. End Sub
    59.  
    60. Public Sub Command1_Click()
    61. Call AddUser(TextBox1.Text, TextBox2.Text, TextBox3.Text)
    62. End Sub
    63.  
    64. Function AddUser(sFirstName As String, sLastName As String, SAge As String) As Boolean
    65.     'On Error GoTo ErrAdd
    66.     AddUser = True
    67.     With Rs
    68.         .AddNew 'adding new record ' GIVE ME ERROR HERE AGAIN
    69.         .Fields("First_Name") = sFirstName
    70.         .Fields("Last_Name") = sLastName
    71.         .Fields("Age") = SAge
    72.         .Update 'this updates the recordset etc.
    73.     End With
    74.    
    75. Label2.Caption = "Recordset Updated..."
    76.  
    77. Exit Function
    78. Label2.Caption = "Recordset Updated 2..."
    79.  
    80. End Function

  18. #18
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MqSQL With VB front end, entering info

    Almost, you missed the first part of my post (sorry, it wasn't too clear!)

    This line (in Search_Click) needs to be moved to General Declarations (the same place as "Dim oConn..." etc.)
    VB Code:
    1. Dim rst As ADODB.Recordset

  19. #19

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: MqSQL With VB front end, entering info

    Ok i took it out, and added it above:
    Now it gives me:

    it says now, object variable, or with block not set


    VB Code:
    1. Public Sub Command1_Click()
    2. Call AddUser(TextBox1.Text, TextBox2.Text, TextBox3.Text)
    3. End Sub
    4.  
    5. Function AddUser(sFirstName As String, sLastName As String, SAge As String) As Boolean
    6.     'On Error GoTo ErrAdd
    7.     AddUser = True
    8.     With rst
    9.         .AddNew 'adding new record ' GIVES THE ERROR HERE AGAIN
    10.         .Fields("First_Name") = sFirstName
    11.         .Fields("Last_Name") = sLastName
    12.         .Fields("Age") = SAge
    13.         .Update 'this updates the recordset etc.
    14.     End With
    15.    
    16. Label2.Caption = "Recordset Updated..."
    17.  
    18. Exit Function
    19. Label2.Caption = "Recordset Updated 2..."
    20.  
    21. End Function

  20. #20
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MqSQL With VB front end, entering info

    The recordset is created in Search_Click, so you need to run that first (either by clicking it manually, or add this to Form_load: Call Search_Click), or use other code in Form_load to create the recordset (basically the code in Search_Click as far as the "rst.Open" line).

  21. #21

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: MqSQL With VB front end, entering info

    Well when i put that call into my form load, it puts all the records into the serach field results box.

    I only wanted to use the search later not right off the bat.

    So i did what you said, but now it gives me the error:
    in the same spot
    Run time error
    Item cannot be found int he collection corresponding to the requested name or ordinal

    VB Code:
    1. Dim oConn As ADODB.Connection
    2. Dim rst As ADODB.Recordset
    3. Dim Server As String
    4. Dim Database As String
    5.  
    6.  
    7.  
    8. Private Sub Form_Load()
    9. ' Connection Strings
    10. ' Visual Basic Program Connection to Sequel Server
    11. Set oConn = New ADODB.Connection
    12. With oConn
    13.     .CursorLocation = adUseClient
    14.     .Open "Provider=SQLOLEDB.1;Server=MANIFEST2;Database=Northwind;UID=sa;PWD=;"
    15.  
    16. End With
    17. Label2.Caption = "Connected..."
    18. Call Search_Click
    19. End Sub
    20.  
    21.  
    22. Private Sub Form_Unload(Cancel As Integer)
    23.     rst.Close
    24.     Set rst = Nothing
    25.     oConn.Close
    26.     Set oConn = Nothing
    27. End Sub
    28.  
    29. ' Button that Runs the Actual Query
    30. Private Sub Search_Click()
    31.     Dim strSql As String, strResults As String
    32.     Dim lngMaxRcs As Long, lngRcs As Long
    33.  
    34. '---- create the sql statement and print it to the immediates window
    35.     strSql = "SELECT [First_Name] + '  '  +  [Last_Name] as FullName,  [Age] FROM [JoeFox] WHERE [First_Name] like '%" & txtSearch.Text & "%'"
    36.     Debug.Print strSql
    37.    
    38. '---- Open the recordset
    39.     Set rst = New ADODB.Recordset
    40.     rst.Open strSql, oConn, 3, 3, 1 'static,optimistic,adCmdText
    41.    
    42.     If rst.EOF Then
    43.         'If you want a message box to come up, then uncomment the code below
    44.         'MsgBox "No records returned", vbOKOnly + vbInformation, "Search"
    45.         txtResults.Text = "No Records Match the Criteria." & vbCrLf & "Please try again."
    46.     Else
    47.         rst.MoveLast
    48.         rst.MoveFirst
    49.         lngMaxRcs = rst.RecordCount
    50.         For lngRcs = 1 To lngMaxRcs
    51.             strResults = strResults & IIf(Len(strResults) > 0, vbCrLf, "") & rst("fullname") & " Age=" & rst("age")
    52.             rst.MoveNext
    53.         Next
    54.         ' This is where the results are displayed
    55.         txtResults.Text = strResults
    56.         Label2.Caption = "Search Completed"
    57.     End If
    58.     ' Closes the Record set so that it dosent us up memory space
    59. End Sub
    60.  
    61. Public Sub Command1_Click()
    62. Call AddUser(TextBox1.Text, TextBox2.Text, TextBox3.Text)
    63. End Sub
    64.  
    65. Function AddUser(sFirstName As String, sLastName As String, SAge As String) As Boolean
    66.     'On Error GoTo ErrAdd
    67.     AddUser = True
    68.     With rst
    69.         .AddNew 'adding new record
    70.         .Fields("First_Name") = sFirstName
    71.         .Fields("Last_Name") = sLastName
    72.         .Fields("Age") = SAge
    73.         .Update 'this updates the recordset etc.
    74.     End With
    75.    
    76. Label2.Caption = "Recordset Updated..."
    77.  
    78. Exit Function
    79. Label2.Caption = "Recordset Updated 2..."
    80.  
    81. End Function

  22. #22
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MqSQL With VB front end, entering info

    Quote Originally Posted by joefox
    Well when i put that call into my form load, it puts all the records into the serach field results box.

    I only wanted to use the search later not right off the bat.
    In that case go for the third option I posted - use code in Form_load to create the recordset, eg:
    VB Code:
    1. Private Sub Form_Load()
    2. ' Connection Strings
    3. ' Visual Basic Program Connection to Sequel Server
    4. Set oConn = New ADODB.Connection
    5. With oConn
    6.     .CursorLocation = adUseClient
    7.     .Open "Provider=SQLOLEDB.1;Server=MANIFEST2;Database=Northwind;UID=sa;PWD=;"
    8.  
    9. End With
    10. Label2.Caption = "Connected..."
    11.  
    12. Dim strSql As String
    13. '---- create the sql statement and print it to the immediates window
    14.     strSql = "SELECT [First_Name] + '  '  +  [Last_Name] as FullName,  [Age] FROM [JoeFox] WHERE [First_Name] like '%" & txtSearch.Text & "%'"
    15.     Debug.Print strSql
    16.    
    17. '---- Open the recordset
    18.     Set rst = New ADODB.Recordset
    19.     rst.Open strSql, oConn, 3, 3, 1 'static,optimistic,adCmdText
    20. '(the recordset is now open)
    21.  
    22. End Sub

    So i did what you said, but now it gives me the error:
    in the same spot
    Run time error
    Item cannot be found int he collection corresponding to the requested name or ordinal
    That error means that the field that you specified is not in the recordset. I have just spotted that in your SQL you combine First_Name and Last_Name (as FullName). This means that neither of these fields are in the recordset!

    There are basically two options, the first is to add these fields to your SQL so they are in the recordset (alternatively you could take out the FullName part, and do the + " " + in your VB code), eg:
    VB Code:
    1. strSql = "SELECT [First_Name] + '  '  +  [Last_Name] as FullName,  [Age], First_Name, Last_Name " _
    2.          & "FROM [JoeFox] WHERE [First_Name] like '%" & txtSearch.Text & "%'"
    or, the second option is to create a recordset (or use an Insert statement) to add the data. attached is a modified version of your code that does this.
    Attached Files Attached Files

  23. #23

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: MqSQL With VB front end, entering info

    It says invalid column name "false"

    VB Code:
    1. Dim strSql As String
    2. '---- create an sql statement that returns no data (as false <> true)
    3.     strSql = "SELECT [First_Name], [Last_Name], [Age] " _
    4.            & "FROM [JoeFox] " _
    5.            & "WHERE false = true" ' GIVES ERROR HERE
    6. '---- Open the recordset
    7.     Set rst = New ADODB.Recordset
    8.     rst.Open strSql, oConn, 3, 3, 1 'static,optimistic,adCmdText
    9.  
    10.     AddUser = True
    11.     With rst
    12.         .AddNew 'adding new record
    13.         .Fields("First_Name") = sFirstName
    14.         .Fields("Last_Name") = sLastName
    15.         .Fields("Age") = SAge
    16.         .Update 'this updates the recordset
    17.     End With
    18.     Label2.Caption = "Recordset Updated..."
    19.  
    20.     ' Closes the Record set so that it dosent us up memory space
    21.     rst.Close
    22.     Set rst = Nothing
    23.  
    24. End Function

  24. #24

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: MqSQL With VB front end, entering info

    I commented out the & "WHERE false = true" ' GIVES ERROR HERE
    And it works

  25. #25
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: MqSQL With VB front end, entering info

    false is just a boolean, it shouldn't have an issue with that!!

    You could replace it with: & "WHERE 1 = 2"

    Having this will make it faster when more records are in the database, as no data will be sent to your computer - without this bit ALL of the data in that table will be sent to your computer.

    I'm glad it's working

  26. #26

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: SOLVED MqSQL With VB front end, entering info

    Another quick question..when i hit the "Update button" i want my caption to say there first and last name + Updated.

    VB Code:
    1. Dim strSql As String
    2. '---- create an sql statement that returns no data (as false <> true)
    3.     strSql = "SELECT [First_Name], [Last_Name], [Age] " _
    4.            & "FROM [JoeFox] " _
    5.            & "WHERE 1 = 2"
    6. '---- Open the recordset
    7.     Set rst = New ADODB.Recordset
    8.     rst.Open strSql, oConn, 3, 3, 1 'static,optimistic,adCmdText
    9.  
    10.     AddUser = True
    11.     With rst
    12.         .AddNew 'adding new record
    13.         .Fields("First_Name") = sFirstName
    14.         .Fields("Last_Name") = sLastName
    15.         .Fields("Age") = SAge
    16.         .Update 'this updates the recordset
    17.     End With
    18.     Label2.Caption = "sFirstName" + " " + "sLastName" + "Entered..." ' THIS JUST PRINTS THE WORKS sFirstname :(
    19.  
    20.     ' Closes the Record set so that it dosent us up memory space
    21.     rst.Close
    22.     Set rst = Nothing
    23.  
    24. End Function

  27. #27

    Thread Starter
    Frenzied Member joefox's Avatar
    Join Date
    Oct 2004
    Posts
    1,318

    Re: SOLVED MqSQL With VB front end, entering info

    I figured it out

    Label2.Caption = TextBox1.Text + " " + TextBox2.Text + "Entered..."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width