Solved

SQL & VBA

Posted on 2011-03-14
3
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

696 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