Results 1 to 6 of 6
  1. #1
    nazimscr is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2012
    Location
    Usa
    Posts
    4

    Add 2 Records to table using one form

    Dear All,

    I would like to save two records to table from one form using vba. Below are the fields in my form and table that will remain same for each of the 2 records
    Main Variables
    TaskID (Primary Key with automatic number)
    TruckNumber
    DriverName
    CurrentKM


    for each of the below variables from the form, 2 records needs to be saved in TyreNumber field in the table


    NewTyreNo
    PreviousTyreNo


    My form name is TaskForm and table name is Tasks

    When the user clicks the save button, i would like to save two records from the same form ie one with NewTyreNo and other with PreviousTyreNo.

    I am using below code to store data for the above two fields:
    Code:
    Dim NewTyreNo As String
    Dim PreviousTyreNo As String
    
    PreviousTyreNo= Me.PreviousTyreNo
    NewTyreNo = Me.NewTyreNo
    Sorry my vba knowledge is limited, i would really appreciate any help in this regard.

    Regards,

    Nazim

  2. #2
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Use code like this behind your Save button to Insert the two records into your Tasks table:

    Code:
    'I'm assuming that TyreNo is a numeric field.
    
    Dim PrevTyre As Long, NewTyre as Long
    
    Me.TxtPreviousTyreNo.SetFocus
    PrevTyre = Me.TxtPreviousTyreNo
    
    Me.TxtNewTyreNo.SetFocus
    NewTyre = Me.TxtNewTyreNo
    
    StrSQL = "INSERT INTO Tasks (TyreNumber) "
    StrSQL = StrSQL & "VALUES (" & NewTyre & "); "
    DoCmd.RunSQL StrSQL
    
    StrSQL = "INSERT INTO Tasks (TyreNumber) "
    StrSQL = StrSQL & "VALUES (" & PrevTyre & "); "
    DoCmd.RunSQL StrSQL
    This should insert two rows of data - each will have a new AutoNumber TaskID, and a TyreNo [either Prev or New].

    Hope this helps. Let me know if there are problems.

  3. #3
    nazimscr is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2012
    Location
    Usa
    Posts
    4
    Hi Robeen,

    Thanks for the code its working well, but one issue, i slightly changed the code to incorporate other field data to the table from the form.

    But one field, ie DriverName is not working which is a text field and all other fields are numeric field.

    strSql = "INSERT INTO Tasks (TyreNumber,CurrentKm,TruckNumber,DriverName) "strSql = strSql & "VALUES (" & NewTyre & "," & Me.CurrentKM & "," & Me.TruckNumber & "," & Me.DriverName & "); "
    DoCmd.RunSQL strSql

  4. #4
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    Using Strings [Text] in one of those queries is a little more tricky because you have to pass double-quotes on either side of the string [DriverName]:
    Try copying the following into your code and see if it works.
    Code:
    strSql = "INSERT INTO Tasks (TyreNumber,CurrentKm,TruckNumber,DriverName) "strSql = strSql & "VALUES (" & NewTyre & "," & Me.CurrentKM & "," & Me.TruckNumber & "," & "'" & Me.DriverName & "'" & "); "
    DoCmd.RunSQL strSql
    Also - here's a web site that belongs to pbaldy - a regular helper on this site.
    There is some information there that I have found very handy.
    http://www.baldyweb.com/

  5. #5
    nazimscr is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2012
    Location
    Usa
    Posts
    4
    Thanks Robeen, its working perfectly now.

  6. #6
    Robeen is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    Mar 2011
    Location
    Tulsa, Oklahoma.
    Posts
    1,596
    I'm glad I could help!! Mark the thread solved - top of page - Thread Tools.
    All the best!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 07-26-2012, 10:51 AM
  2. Replies: 5
    Last Post: 03-02-2012, 02:47 PM
  3. More records in table than form
    By GraemeG in forum Access
    Replies: 2
    Last Post: 03-11-2011, 02:44 PM
  4. Replies: 3
    Last Post: 01-24-2011, 12:04 PM
  5. Replies: 0
    Last Post: 02-24-2010, 12:56 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums