Hi,
When I run this code (from a button on a form, in relation to a subform record) I receive this error:
Run-time error '13':
Type mismatch
When I debug.print the SQL and put it into a blank query, the query runs and appends a new record to the appropriate table.
I am perplexed. Anyone advise on why this might be happening?
--------------
VBA
--------------
Dim mySQL As String
Dim lngContact As Long
Dim lngDiscount As Long
Dim strMessage As String
Dim strUsername As String
Dim dtDateNow As Date
lngContact = Me!ContactID
dtDateNow = Now()
lngDiscount = 4
strMessage = "New member Discount"
strUsername = fOSUserName()
mySQL = "INSERT INTO tblCONTACTDISCOUNTINDEX ( ContactID, DiscountID, Description, AddedBy, DateAdded ) "
mySQL = mySQL & "SELECT " & lngContact & " AS Expr1, " & lngDiscount & " AS Expr2, "
mySQL = mySQL & "'" & strMessage & "'" & " AS Expr3, " & "'" & strUsername & "'" & " AS Expr4, " & "'" & dtDateNow & "'" & " AS Expr5"
Debug.Print mySQL
DoCmd.SetWarnings False
DoCmd.RunCommand mySQL
DoCmd.SetWarnings True
-----------------------
working SQL from Debug.print
-----------------------
INSERT INTO tblCONTACTDISCOUNTINDEX ( ContactID, DiscountID, Description, AddedBy, DateAdded ) SELECT 14433 AS Expr1, 4 AS Expr2, 'New member Discount' AS Expr3, 'testUser' AS Expr4, '24/09/2012 09:44:01' AS Expr5
When I run this code (from a button on a form, in relation to a subform record) I receive this error:
Run-time error '13':
Type mismatch
When I debug.print the SQL and put it into a blank query, the query runs and appends a new record to the appropriate table.
I am perplexed. Anyone advise on why this might be happening?
--------------
VBA
--------------
Dim mySQL As String
Dim lngContact As Long
Dim lngDiscount As Long
Dim strMessage As String
Dim strUsername As String
Dim dtDateNow As Date
lngContact = Me!ContactID
dtDateNow = Now()
lngDiscount = 4
strMessage = "New member Discount"
strUsername = fOSUserName()
mySQL = "INSERT INTO tblCONTACTDISCOUNTINDEX ( ContactID, DiscountID, Description, AddedBy, DateAdded ) "
mySQL = mySQL & "SELECT " & lngContact & " AS Expr1, " & lngDiscount & " AS Expr2, "
mySQL = mySQL & "'" & strMessage & "'" & " AS Expr3, " & "'" & strUsername & "'" & " AS Expr4, " & "'" & dtDateNow & "'" & " AS Expr5"
Debug.Print mySQL
DoCmd.SetWarnings False
DoCmd.RunCommand mySQL
DoCmd.SetWarnings True
-----------------------
working SQL from Debug.print
-----------------------
INSERT INTO tblCONTACTDISCOUNTINDEX ( ContactID, DiscountID, Description, AddedBy, DateAdded ) SELECT 14433 AS Expr1, 4 AS Expr2, 'New member Discount' AS Expr3, 'testUser' AS Expr4, '24/09/2012 09:44:01' AS Expr5