Results 1 to 10 of 10

Thread: Access Update many records at once

  1. #1

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Access Update many records at once

    I have posted a similar question to this before so it's possible that given time I will solve this myself. But the help would be real good all the same.

    I am working on a database project where there is a table of "global" values. It has two fields PK and Value both text. PK is the Primary Key (Duh).

    The form is designed from unbound controls (mainly textboxes and comboboxes) one for each record. I have set the tag value to hold the PK of the destination record and the control clearly will hold the value for the Value field.

    The plan is to have a module that contains a function of sub that will cycle through the controls on the form and add the values to the correct record.

    Not knowing what to do I have started to plan out said Sub with the result that I have pritty much of the code fairly well written. However I am fairly new to the whole dealing with objects thing and would apriciate some feed back on my code (untested) and some clues on how to do the update records.

    VB Code:
    1. Sub update(frm As Form)
    2.     On Error Resume Next
    3.    
    4.     Dim Ctl As Control
    5.     Dim Obj As Object
    6.     Dim CtlType As String
    7.     Dim MeRecord As String
    8.     Dim MeValue As String
    9.  
    10.     'set the controls' values to the DB using the
    11.     'Tag property to locate the PK
    12.  
    13.     For Each Ctl In frm.Controls
    14.        
    15.         If CtlType = "Label" Then
    16.             'do nothing
    17.         ElseIf CtlType = "TextBox" Then
    18.             MeRecord = Ctl.Tag
    19.             MeValue = Ctl.Text
    20.         ElseIf CtlType = "ComboBox" Then
    21.             MeRecord = Ctl.Tag
    22.             MeValue = Ctl.Value
    23.         Else
    24.             'Wonder what the hell is going on... Buttons
    25.         End If
    26.         'Take to values and now add them to the data base
    27.         'Um find record where PK=MeRecord Set Value = MeValue
    28.     Next
    29. End Sub


    if you really want to know the same topic post in this forum was called: Access Module Code: rst.movenext I assume... but that aside I'm a bit stuck today... the answers in other thread might help a bit but it deals with Access to Word more'n anything else.

    Thanks in advance
    ?
    'What's this bit for anyway?
    For Jono

  2. #2
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    To update, either open a recordset using the PK and then edit the record, or create an UPDATE sql statement and put the PK in the 'where' clause to get the correct record.

    Are all of the records in the same table? If not you are going to have problems making this generic.

  3. #3

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    The table is made of two fields

    PK : TEXT
    Value : TEXT

    That's it.

    All records are represented on the form at once.
    ?
    'What's this bit for anyway?
    For Jono

  4. #4
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Well an update SQL statement would look like this:

    Code:
    Dim strSQL as String
    
    strSQL = "UPDATE tblName SET Value = '" & MeValue & "' " & _
                   "WHERE PK = '" & MeRecord & "' "
    
     Connection.Execute strSQL

  5. #5

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774

    Um...

    That would be ok I guess - the idea of the code is that I will not know what the PKs are (stored in the TAG value) and do not know what the controls will actually be other than limited to text box and combo at this stage. So I need to loop through and get the info out and write it into the table. I would imagine that this would work then??

    VB Code:
    1. Sub update(frm As Form)
    2.     On Error Resume Next
    3.    
    4.     Dim Ctl As Control
    5.     Dim Obj As Object
    6.     Dim CtlType As String
    7.     Dim MeRecord As String
    8.     Dim MeValue As String
    9.     Dim strSQL as String
    10.  
    11.  
    12.     'set the controls' values to the DB using the
    13.     'Tag property to locate the PK
    14.  
    15.     For Each Ctl In frm.Controls
    16.        
    17.         If CtlType = "Label" Then
    18.             'do nothing
    19.         ElseIf CtlType = "TextBox" Then
    20.             MeRecord = Ctl.Tag
    21.             MeValue = Ctl.Text
    22.         ElseIf CtlType = "ComboBox" Then
    23.             MeRecord = Ctl.Tag
    24.             MeValue = Ctl.Value
    25.         Else
    26.             'do nothing with this one
    27.         End If
    28.         strSQL = "UPDATE tblName SET Value = '" & MeValue & "' " & _
    29.                "WHERE PK = '" & MeRecord & "' "
    30.     Next
    31. End Sub

    is the rest of the code good I wonder.... well time to find out!

    EDIT: SPot the error!
    ?
    'What's this bit for anyway?
    For Jono

  6. #6

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    The big question is really the moveing through the collection of objects on a form and doing X for each Y in Z kinda thing.
    ?
    'What's this bit for anyway?
    For Jono

  7. #7
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    Code:
    Dim ctl as Control
    
    For each ctl in Me.Controls
    
    If TypeOf ctl is Textbox Then
      
       'do stuff
    ElseIf TypeOf ctl is ComboBox
    
       'do more stuff
    End If
    etc etc

  8. #8

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    I was close... thankyou if not for your timely post I would have remained only very close ...maybe forever.
    ?
    'What's this bit for anyway?
    For Jono

  9. #9

    Thread Starter
    Fanatic Member Matt_T_hat's Avatar
    Join Date
    Dec 2001
    Location
    '76 Male Body Evil-Errors: 666
    Posts
    774
    umm... ERROR: module is not a valid type??

    any clues??
    ?
    'What's this bit for anyway?
    For Jono

  10. #10
    Hyperactive Member Granty's Avatar
    Join Date
    Mar 2001
    Location
    London
    Posts
    439
    ???

    You are putting this in a form right?

    Post the code that is erroring pls?

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