Results 1 to 17 of 17

Thread: ADODB & Loops Gallor!

  1. #1

    Thread Starter
    Fanatic Member cid's Avatar
    Join Date
    Nov 2002
    Location
    Fort Worth, Texas
    Posts
    854

    ADODB & Loops Gallor!

    VB Code:
    1. Option Explicit
    2.     Dim ADOCn As adodb.Connection
    3.     Dim rsvalues As adodb.Recordset
    4.     Dim ConnString As String
    5.     Dim rsrecord As adodb.Record
    6.     Dim i As Integer
    7.     Dim c As Control

    VB Code:
    1. Private Sub DataConnect_Click()
    2.    
    3.  
    4.    On Error GoTo DataConnect_Click_Error
    5.  
    6. CommonDialog1.Filter = "Access Database (*.mdb)|*.mdb|"
    7. CommonDialog1.ShowOpen
    8.   ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    9.   "Data Source=" & CommonDialog1.FileName & ";" & _
    10.   "Persist Security Info=False"
    11.      Set ADOCn = New adodb.Connection
    12.      ADOCn.ConnectionString = ConnString
    13.      ADOCn.Open
    14.        Set rsvalues = New adodb.Recordset
    15.        rsvalues.Open "SELECT * FROM Permits", ADOCn, adOpenKeyset, adLockOptimistic, adCmdText
    16. For i = 0 To rsvalues.Fields.Count - 1
    17. For Each c In Me.Controls
    18.  If c.Name = "txt" & rsvalues.Fields(i).Name Then
    19.   c.Text = CStr(rsvalues.Fields(i).Value & "")
    20.    Exit For
    21.   End If
    22.  Next c
    23. rsvalues.MoveFirst
    24.  On Error GoTo 0
    25.  Exit Sub
    26. Next
    27. DataConnect_Click_Error:
    28.  
    29.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure DataConnect_Click of Form Permit1"
    30.  End Sub

    VB Code:
    1. Private Sub datanew_click()
    2.         For i = 0 To rsvalues.Fields.Count - 1
    3.           For Each c In Me.Controls
    4.                  
    5.           rsvalues.AddNew
    6.       If c.Name = "txt" & rsvalues.Fields(i).Name Then
    7.         Select Case rsvalues.Fields(i).Type
    8.            Case adodb.DataTypeEnum.adVarChar, adodb.DataTypeEnum.adChar
    9.                 rsvalues.Fields(i).Value = c.Text
    10.            Case adodb.DataTypeEnum.adDate
    11.                 rsvalues.Fields(i).Value = CDate(c.Text)
    12.            Case adodb.DataTypeEnum.adInteger
    13.                 rsvalues.Fields(i).Value = CInt(c.Text)
    14.            rsvalues.Update
    15.            End Select
    16.            End If
    17.            Exit For
    18.              Next c
    19.              Next i
    20.                
    21.                
    22.  
    23.     For Each c In Me.Controls
    24.      If TypeOf c Is TextBox Then
    25.     c.Text = vbNullString
    26. End If
    27. Next
    28. End Sub

    VB Code:
    1. Private Sub datadel_click()
    2. Set rsrecord = New adodb.Record
    3. rsrecord.DeleteRecord
    4. rsvalues.Update
    5. End Sub
    6.  
    7. Private Sub FillData_Click()
    8.  
    9.   For i = 0 To rsvalues.Fields.Count - 1
    10.     For Each c In Me.Controls
    11.       If c.Name = "txt" & rsvalues.Fields(i).Name Then
    12.         Select Case rsvalues.Fields(i).Type
    13.         Case adText, adChar
    14.             rsvalues.Fields(i).Value = c.Text
    15.         Case adDate
    16.             rsvalues.Fields(i).Value = CDate(c.Text)
    17.         Case adInteger
    18.             rsvalues.Fields(i).Value = CInt(c.Text)
    19.        End Select
    20.       End If
    21.       Exit For
    22.     Next c
    23.   Next i
    24.  
    25.   For Each c In Me.Controls
    26.     If TypeOf c Is TextBox Then
    27.       c.Text = vbNullString
    28.     End If
    29.   Next c
    30. End Sub
    31.  
    32. Private Sub FillText_Click()
    33. For i = 0 To rsvalues.Fields.Count - 1
    34.   For Each c In Me.Controls
    35.     If c.Name = "txt" & rsvalues.Fields(i).Name Then
    36.       c.Text = CStr(rsvalues.Fields(i).Value & "")
    37.       Exit For
    38.     End If
    39.   Next c
    40. Next i
    41. End Sub

    Few problems, my DataNew doesnt save the information in the textboxes. And DataDel IS A FRICKING MESS!

    www.google.com - Pay Tribute.

    Always Listening To: Thrice

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: ADODB & Loops Gallor!

    datanew:

    move the .Update outside the for each control loop.

    Code:
               For Each c In Me.Controls
                      
              rsvalues.AddNew
          If c.Name = "txt" & rsvalues.Fields(i).Name Then
            Select Case rsvalues.Fields(i).Type
               Case adodb.DataTypeEnum.adVarChar, adodb.DataTypeEnum.adChar
                    rsvalues.Fields(i).Value = c.Text
               Case adodb.DataTypeEnum.adDate
                    rsvalues.Fields(i).Value = CDate(c.Text)
               Case adodb.DataTypeEnum.adInteger
                    rsvalues.Fields(i).Value = CInt(c.Text)
               End Select
               End If
               Exit For
                 Next c
               rsvalues.Update
    That way it only does the update AFTer all fields have been updated.

    And yeah, DataDel doesn't do anything. Two ways to delete. 1) Find the record in the recorset, then do a rs.Delete on it. or 2) create a DELETE SQL command and execute that.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member cid's Avatar
    Join Date
    Nov 2002
    Location
    Fort Worth, Texas
    Posts
    854

    Re: ADODB & Loops Gallor!

    I updated my code and it is still doing the same thing:

    VB Code:
    1. Private Sub datanew_click()
    2.         For i = 0 To rsvalues.Fields.Count - 1
    3.          For Each c In Me.Controls
    4.                  
    5.           rsvalues.AddNew
    6.       If c.Name = "txt" & rsvalues.Fields(i).Name Then
    7.         Select Case rsvalues.Fields(i).Type
    8.            Case adodb.DataTypeEnum.adVarChar, adodb.DataTypeEnum.adChar
    9.                 rsvalues.Fields(i).Value = c.Text
    10.            Case adodb.DataTypeEnum.adDate
    11.                 rsvalues.Fields(i).Value = CDate(c.Text)
    12.            Case adodb.DataTypeEnum.adInteger
    13.                 rsvalues.Fields(i).Value = CInt(c.Text)
    14.            End Select
    15.            End If
    16.            Exit For
    17.              Next c
    18.            rsvalues.Update
    19.                
    20.                
    21.  
    22.     For Each c In Me.Controls
    23.      If TypeOf c Is TextBox Then
    24.     c.Text = vbNullString
    25. End If
    26. Next
    27. Next
    28. rsvalues.MoveNext
    29. End Sub

    www.google.com - Pay Tribute.

    Always Listening To: Thrice

  4. #4

    Thread Starter
    Fanatic Member cid's Avatar
    Join Date
    Nov 2002
    Location
    Fort Worth, Texas
    Posts
    854

    Re: ADODB & Loops Gallor!

    ...
    Ok I just figured out that my AddNewRecord is not only creating 1 record, but everytime I hit the button it creates about 70!.

    www.google.com - Pay Tribute.

    Always Listening To: Thrice

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: ADODB & Loops Gallor!

    Sorry about that. I can see now what you are doing......

    VB Code:
    1. Private Sub datanew_click()
    2.  
    3.   'Add a new record
    4.   rsvalues.AddNew
    5.  
    6.   'Loop through each field
    7.   For i = 0 To rsvalues.Fields.Count - 1
    8.     'Loop through each control
    9.     For Each c In Me.Controls
    10.       'If the control name matched the field name
    11.       If c.Name = "txt" & rsvalues.Fields(i).Name Then
    12.         'Set it based on the data type
    13.         Select Case rsvalues.Fields(i).Type
    14.            Case adodb.DataTypeEnum.adVarChar, adodb.DataTypeEnum.adChar
    15.                 rsvalues.Fields(i).Value = c.Text
    16.            Case adodb.DataTypeEnum.adDate
    17.                 rsvalues.Fields(i).Value = CDate(c.Text)
    18.            Case adodb.DataTypeEnum.adInteger
    19.                 rsvalues.Fields(i).Value = CInt(c.Text)
    20.         End Select
    21.       End If 'end if field matches control
    22.     Next c 'Get the next control
    23.   Next i 'Get the next field
    24.  
    25.   'Update the recordset
    26.   rsvalues.Update
    27.  
    28.   'Loop through the controls and blank each one out
    29.   For Each c In Me.Controls
    30.     If TypeOf c Is TextBox Then
    31.       c.Text = vbNullString
    32.     End If
    33.   Next
    34.  
    35.   'Move to the first record
    36.   rsvalues.MoveFirst
    37.  
    38. End Sub

    See if that works better. Cleaned it up some, rearranged afew things and added comments

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Fanatic Member cid's Avatar
    Join Date
    Nov 2002
    Location
    Fort Worth, Texas
    Posts
    854

    Re: ADODB & Loops Gallor!

    type mismatch:

    VB Code:
    1. rsvalues.Fields(i).Value = CDate(c.Text)

    It seems to actually be doing its job now, thank you for the code thus far. After I finish this adding problem, my project will almost be done

    www.google.com - Pay Tribute.

    Always Listening To: Thrice

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: ADODB & Loops Gallor!

    yeah, if c.Text isn't a date compatible format or value, converting it to Date will fail. Probably should do a IsDate() check on it first, then set it if it's valid else set the field to NULL.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Fanatic Member cid's Avatar
    Join Date
    Nov 2002
    Location
    Fort Worth, Texas
    Posts
    854

    Re: ADODB & Loops Gallor!

    how do i do a IsDate()? This is my second day on ADO

    www.google.com - Pay Tribute.

    Always Listening To: Thrice

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: ADODB & Loops Gallor!

    IsDate() is a VB function.... has nothing to do with ado...

    VB Code:
    1. if IsDate(c.Text) Then
    2.   rsvalues.Fields(i).Value = CDate(c.Text)
    3. Else
    4.   rsvalues.Fields(i).Value = Null 'Or what ever default value you want
    5. End If
    Stick that inside your select case where you are setting the date datatype.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Fanatic Member cid's Avatar
    Join Date
    Nov 2002
    Location
    Fort Worth, Texas
    Posts
    854

    Re: ADODB & Loops Gallor!

    Thank you techgnome... Now it wont create 70 new records.
    Last problem standing is that it will not append the data to the fields

    VB Code:
    1. 'Add a new record
    2.   rsvalues.AddNew
    3.  
    4.   'Loop through each field
    5.   For i = 0 To rsvalues.Fields.Count - 1
    6.     'Loop through each control
    7.     For Each c In Me.Controls
    8.       'If the control name matched the field name
    9.       If c.Name = "txt" & rsvalues.Fields(i).Name Then
    10.         'Set it based on the data type
    11.         Select Case rsvalues.Fields(i).Type
    12.            Case ADODB.DataTypeEnum.adVarChar, ADODB.DataTypeEnum.adChar
    13.                 rsvalues.Fields(i).Value = c.Text
    14.           If IsDate(c.Text) Then
    15.             rsvalues.Fields(i).Value = CDate(c.Text)
    16.             Else
    17.             rsvalues.Fields(i).Value = Null 'Or what ever default value you want
    18.             End If
    19.            Case ADODB.DataTypeEnum.adInteger
    20.                 rsvalues.Fields(i).Value = CInt(c.Text)
    21.         End Select
    22.       End If 'end if field matches control
    23.     Next c 'Get the next control
    24.      Next i 'Get the next field
    25.  
    26.   'Update the recordset
    27.   rsvalues.Update
    28.  
    29.   'Loop through the controls and blank each one out
    30.   For Each c In Me.Controls
    31.     If TypeOf c Is TextBox Then
    32.       c.Text = vbNullString
    33.     End If
    34.   Next
    35.  
    36.   'Move to the next record
    37.   rsvalues.MoveNext

    www.google.com - Pay Tribute.

    Always Listening To: Thrice

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

    Re: ADODB & Loops Gallor!

    How many threads have you created for the same question?? This is confusing and annoying - I have to read 3 threads to find out what the current situation is!

    Last problem standing is that it will not append the data to the fields
    What exactly do you mean by this?

    If you mean that it is adding a new record when you want to update the current one, just remove/comment out the "rsvalues.AddNew" line.

  12. #12

    Thread Starter
    Fanatic Member cid's Avatar
    Join Date
    Nov 2002
    Location
    Fort Worth, Texas
    Posts
    854

    Re: ADODB & Loops Gallor!

    The only problem that remains is making the program bind the data to a field in the database. It will create new records, but it doesnt save the old ones.

    VB Code:
    1. Private Sub datanew_click()
    2.    'Add a new record
    3.   FillData_Click
    4.   rsvalues.AddNew
    5.  
    6.   'Loop through each field
    7.   For i = 0 To rsvalues.Fields.Count - 1
    8.     'Loop through each control
    9.     For Each c In Me.Controls
    10.       'If the control name matched the field name
    11.       If c.Name = "txt" & rsvalues.Fields(i).Name Then
    12.         'Set it based on the data type
    13.         Select Case rsvalues.Fields(i).Type
    14.            Case adodb.DataTypeEnum.adVarChar, adodb.DataTypeEnum.adChar
    15.                 rsvalues.Fields(i).Value = c.Text
    16.           If IsDate(c.Text) Then
    17.             rsvalues.Fields(i).Value = CDate(c.Text)
    18.             Else
    19.             rsvalues.Fields(i).Value = Null 'Or what ever default value you want
    20.             End If
    21.            Case adodb.DataTypeEnum.adInteger
    22.                 rsvalues.Fields(i).Value = CInt(c.Text)
    23.         End Select
    24.       End If 'end if field matches control
    25.     Next c 'Get the next control
    26.      Next i 'Get the next field
    27.  
    28.   'Update the recordset
    29.   rsvalues.Update
    30.  
    31.   'Loop through the controls and blank each one out
    32.   For Each c In Me.Controls
    33.     If TypeOf c Is TextBox Then
    34.       c.Text = vbNullString
    35.     End If
    36.   Next
    37.  
    38.   'Move to the next record
    39.   rsvalues.MoveNext
    40. End Sub
    41.  
    42.  
    43. Private Sub FillData_Click()
    44.  
    45.     For i = 0 To rsvalues.Fields.Count - 1
    46.     'Loop through each control
    47.     For Each c In Me.Controls
    48.       'If the control name matched the field name
    49.       If c.Name = "txt" & rsvalues.Fields(i).Name Then
    50.         'Set it based on the data type
    51.         Select Case rsvalues.Fields(i).Type
    52.            Case adodb.DataTypeEnum.adVarChar, adodb.DataTypeEnum.adChar
    53.                 rsvalues.Fields(i).Value = c.Text
    54.           If IsDate(c.Text) Then
    55.             rsvalues.Fields(i).Value = CDate(c.Text)
    56.             Else
    57.             rsvalues.Fields(i).Value = Null 'Or what ever default value you want
    58.             End If
    59.            Case adodb.DataTypeEnum.adInteger
    60.                 rsvalues.Fields(i).Value = CInt(c.Text)
    61.         End Select
    62.       End If 'end if field matches control
    63.     Next c 'Get the next control
    64.      Next i 'Get the next field
    65.  
    66.   'Update the recordset
    67.   rsvalues.Update
    68.  
    69. End Sub

    See anything wrong here? It should be updating perfectly

    www.google.com - Pay Tribute.

    Always Listening To: Thrice

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

    Re: ADODB & Loops Gallor!

    It looks to me as if the FillData_click sub should save the values in the textboxes to the current record. ADO has no method to initiate Editing (DAO had rs.edit, but ADO assumes as soon as you set a value) so the code you have should work.

    What is it actually doing? Is it being saved anywhere in the database table?

  14. #14

    Thread Starter
    Fanatic Member cid's Avatar
    Join Date
    Nov 2002
    Location
    Fort Worth, Texas
    Posts
    854

    Re: ADODB & Loops Gallor!

    Quote Originally Posted by si_the_geek
    It looks to me as if the FillData_click sub should save the values in the textboxes to the current record. ADO has no method to initiate Editing (DAO had rs.edit, but ADO assumes as soon as you set a value) so the code you have should work.

    What is it actually doing? Is it being saved anywhere in the database table?
    No, all it does it create a new blank record and discards anything i type.

    www.google.com - Pay Tribute.

    Always Listening To: Thrice

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

    Re: ADODB & Loops Gallor!

    "Filldata" does not have appropriate code to add a record, "datanew" does however.

    I think you should upload your code file(s), so that we can see what issues you have - as something isn't right here!

  16. #16

    Thread Starter
    Fanatic Member cid's Avatar
    Join Date
    Nov 2002
    Location
    Fort Worth, Texas
    Posts
    854

    Re: ADODB & Loops Gallor!

    here is the form and the database in which the form operates on

    :Check Attachments:
    Attached Files Attached Files

    www.google.com - Pay Tribute.

    Always Listening To: Thrice

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

    Re: ADODB & Loops Gallor!

    After deciphering your dodgy naming (of controls & subs) and strange indenting, here's a few issues for you:

    In "DataConnect_Click": rather than having a new loop to fill the values (which shouldn't have a .Movefirst !), just Call the approriate sub instead (fillText_Click).

    Behind the button labeled * , your code is:
    VB Code:
    1. Label13.Caption = "Added New Record"
    2. datadel_click
    ..do you see a problem here? the same for the button *.* :
    VB Code:
    1. Label13.Caption = "Deleted Record"
    2. datanew_click

    At the end of "FillData_Click", you seem to have removed the "rs.values.update", which is the part that actually writes to the DB

    "datanew_click": you call FillData_Click, which saves the textbox values to the current record, then you immediately add a new record and save those same values to it, and then clear the boxes. What you should do instead is:
    • start with "FillData_Click" if you want.
    • initiate the Addnew
    • clear the textboxes
    • have another event (possibly a new button) which calls FillData_Click.

    I would personally have a button for saving, which will cover both of these options - note that as things currently stand you do not do anything to save when you move to a different record (you could call FillData_Click before any move commands).

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