Solved

SQL & VBA

Posted on 2011-03-14
3
276 Views
Last Modified: 2012-06-21
Hello All,
I am going to try to explain this the best way I know how.  I am inserting data into a table from a query via SQL.  Before I run the SQL, I am trapping a value in another table.  I then need to get that trapped value into the table from SQL as it builds the records. That trapped value will change from record to record.  My code is below and the InvoiceNo is the trapped value that I need to insert.  Hope that makes sense...Thank you so much in advance.

Paula
Dim sqltext2 As String
Dim rlst2 As String
Dim strFac As String
Dim strPO As Integer
Dim strInv As String
    CurrentDb.Execute "DELETE * FROM [tblLawsonStep2]", dbFailOnError
    CurrentDb.Execute "DELETE * FROM [tblReconTemp]", dbFailOnError
    DoCmd.OpenQuery "qryRecontoTempRecon"
    Set rs1 = CurrentDb.OpenRecordset("SELECT * FROM [tblReconTemp]")
    If Not rs1 Is Nothing Then
        rs1.MoveFirst
        Do While Not rs1.EOF
            rlst2 = ""
            rlst2 = Chr(34) & rs1.Fields("[ReceiverNo]").Value & Chr(34)
            strFac = rs1.Fields("[Facility]").Value
            strPO = rs1.Fields("[PO]").Value
            strInv = rs1.Fields("InvoiceNo").Value
            rlst2 = Mid(rlst2, 2, Len(rlst2) - 2)
            'MsgBox rlst2
            sqltext2 = "INSERT INTO [tblLawsonStep2] SELECT * FROM [qryReceivedAllLawson]" & _
            " WHERE (((qryReceivedAllLawson.Facility)= " & strFac & ") AND ((qryReceivedAllLawson.[PO#])= " & strPO & ") AND ((qryReceivedAllLawson.ReceiverNo) In (" & rlst2 & ")));"
            'MsgBox sqltext2
            DoCmd.RunSQL sqltext2
            
            rs1.MoveNext
        Loop
            'MsgBox rlst2
    End If

Open in new window

0
Comment
Question by:Monterey
  • 2
3 Comments
 
LVL 77

Accepted Solution

by:
peter57r earned 500 total points
ID: 35130707
I'm not sure I have understood correctly but it seems like you should use..

 sqltext2 = "INSERT INTO [tblLawsonStep2] SELECT *, '" & strinv & "' FROM [qryReceivedAllLawson]" & _

Backup before testing.
0
 

Author Comment

by:Monterey
ID: 35130765
OMG - that did it!  Brilliant...thanks so much...one thing I had to add was the field that I wanted the value to go into...

"INSERT INTO [tblLawsonStep2] SELECT *, '" & strInv & "' AS InvoiceNumber FROM [qryReceivedAllLawson]" & _

Thanks for the quick response, it is greatly appreciated!

Paula
0
 

Author Closing Comment

by:Monterey
ID: 35130769
Quick response...answered the question exactly!  Brilliant!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question