Solved

SQL & VBA

Posted on 2011-03-14
3
274 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now