Results 1 to 3 of 3
  1. #1
    Colargol is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    1

    Insert into table from recordset

    Hi,

    How do I make the variable trans to be inserted into table Bank2 ?

    sql = "INSERT INTO Bank2(Field1)Values(trans);"

    I am using access 2010.

    Private Sub Command3_Click()

    On Error GoTo Err_MyProc
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Set db = CurrentDb()
    Set rst = db.OpenRecordset("Bank", dbOpenDynaset)
    Dim a, b, c As String
    Dim sql As String
    Dim LResult As String
    Dim trans As String

    With rst
    '.MoveLast
    .MoveFirst

    sql = "INSERT INTO Bank2(Field1)Values(trans);"


    Do While .EOF = False
    LResult = Left(rst!field1, 6)
    If LResult = "940SWI" Then
    a = rst!field1
    b = 0
    '!Rec_Group = AssignGroup(!Rec_Type)


    Else
    a = a + rst!field1
    b = b + 1
    c = a
    End If
    .MoveNext
    If LResult = "940SWI" Then
    trans = c
    DoCmd.RunSQL sql
    End If
    Loop

    End With


    rst.Close 'Close what you opened.

    Exit_MyProc:
    Set rst = Nothing 'Deassign all objects.
    Set db = Nothing
    Exit Sub

    Err_MyProc:
    'Error handler here.
    Resume Exit_MyProc


    End Sub

    Regards
    Ronny

  2. #2
    hertfordkc is offline 18 year novice
    Windows XP Access 2007
    Join Date
    Mar 2011
    Posts
    481

    I suggest that you use .Addnew, & .Update to create your records.

    See http://allenbrowne.com/ser-29.html and other of Allen Browne's help files.

    I find it easier to work with .Addnew, & .Update than to deal with SQL.

  3. #3
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Code:
    Private Sub Command3_Click()
    
       On Error GoTo Err_MyProc
       Dim db As DAO.Database
       Dim rst As DAO.Recordset
       Set db = CurrentDb()
       Set rst = db.OpenRecordset("Bank", dbOpenDynaset)
    
       'NOTE: a & b are declared as Variant
       ' if you want a, b & c declared as strings then must use
       '------------------------
       'Dim a As String, b As String, c As String
       '------------------------
    
       Dim a, b, c As String
    
       Dim sql As String
       Dim LResult As String
       Dim trans As String
    
       With rst
          '.MoveLast
          .MoveFirst
    
          Do While .EOF = False
             LResult = Left(rst!field1, 6)
             If LResult = "940SWI" Then
                a = rst!field1
                b = 0
                '!Rec_Group = AssignGroup(!Rec_Type)
             Else
                a = a + rst!field1
                b = b + 1
                c = a
             End If
             .MoveNext
             If LResult = "940SWI" Then
                trans = c
                sql = "INSERT INTO Bank2(Field1)Values('" & Nz(trans, "") & "');"
                db.Execute sql, dbFailOnError
             End If
          Loop
    
       End With
    
    
       rst.Close   'Close what you opened.
    
    Exit_MyProc:
       Set rst = Nothing   'Deassign all objects.
       Set db = Nothing
       Exit Sub
    
    Err_MyProc:
       'Error handler here.
       Resume Exit_MyProc
    
    
    End Sub
    BTW, You do have a major flaw in your logic.... "trans" will always be zero..

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

Similar Threads

  1. Replies: 25
    Last Post: 01-29-2013, 08:49 AM
  2. Replies: 1
    Last Post: 12-28-2010, 11:24 AM
  3. Replies: 2
    Last Post: 12-22-2010, 01:46 PM
  4. Replies: 0
    Last Post: 02-24-2010, 12:56 AM
  5. Replies: 3
    Last Post: 02-16-2010, 10:43 PM

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