Use Select Case to change field in DoCmd.RunSQL "INSERT INTO...

TimTDP

Registered User.
Local time
Today, 19:52
Joined
Oct 24, 2008
Messages
213
I have the following code
Code:
DoCmd.RunSQL "INSERT INTO tblInvoicePrepayment (InvoiceId, PrePaymentValue, PrePaymentDate, PrePaymentCash, Driver) VALUES " & _
                 "(" & Me.InvoiceId & "," & sngInvoiceBalanceOutstanding & "," & Me.InvoiceDate & ", True, True)"

In this code the field PrePaymentCash is updated.
However it could also be PrePaymentCheque or PrePaymentEFT, or PrePaymentCard!

At the moment I have 4 versions of the same code, with only the field name changing.

I want to use a Select Case statement to determine which field to update and then use that field in the code. This will be much more efficient!

How would I do this?
 
Create a String for the SQL.. Something along the lines of..
Code:
Dim strSQL As String, fldName As String
Select Case someThing
    Case relatedToPrePaymentValue
        fldName = "PrePaymentValue"
    Case relatedToPrePaymentDate
        fldName = "PrePaymentDate"
    Case relatedToPrePaymentCash
        fldName = "PrePaymentCash"
End Select

strSQL = "INSERT INTO tblInvoicePrepayment (" & fldName & ") VALUES " & _
            "(" & Me.someNumberControl & ")"

DoCmd.RunSQL strSQL
 

Users who are viewing this thread

Back
Top Bottom