I always try to use parameters, they allow me to lock down the types and values being passed to the query.OH! You mean the values are not the order they're declared but the order they're written after! I wondered why I needed to rewrite them! Thank you so much. This is great!
' Using Value for TheName parameter to allow Null
With CurrentDb.CreateQueryDef(vbNullString, _
"PARAMETERS " & _
"[AccountID] Long, [TheName] Value, " & _
"[TheDate] DateTime, [TheValue] IEEEDouble; " & _
"INSERT INTO tblTransactions " & _
"( [fAccountID], [fNameID], [CkDate], [Num] ) " & _
"VALUES " & _
"([AccountID],[TheName],[TheDate],[TheValue]);")
.Parameters(0) = CLng(Nz(Me.cboAccount.Value, 0))
.Parameters(1) = IIf(Trim$(Nz(Frm.cboFullName.Value, vbNullString)) = vbNullString, _
Null, _
Frm.cboFullName.Value)
.Parameters(2) = DateValue(Nz(Frm.CkDate, Now))
.Parameters(3) = CDbl(Nz(Frm.Num, 0))
.Execute dbFailOnError
End With
IMO, you don't need to define the PARAMETERS.I always try to use parameters, they allow me to lock down the types and values being passed to the query.
True, the PARAMETERS section is not needed, but I like to be complete. With defined and typed parameters, passing improper types to the parameters will fail in the code instead of during the query execution.IMO, you don't need to define the PARAMETERS.
If you pass a wrong parameter type it will be ignored (Null value will be saved).
With CurrentDb.CreateQueryDef(vbNullString, _
"INSERT INTO tblTransactions " & _
"( [fAccountID], [fNameID], [CkDate], [Num] ) " & _
"VALUES " & _
"([P0],[P1],[P2],[P3]);")
.Parameters(0) = CLng(Nz(Me.cboAccount.Value, 0))
.Parameters(1) = IIf(Trim$(Nz(Frm.cboFullName.Value, vbNullString)) = vbNullString, _
Null, _
Frm.cboFullName.Value)
.Parameters(2) = DateValue(Nz(Frm.CkDate, Now))
.Parameters(3) = CDbl(Nz(Frm.Num, 0))
.Execute dbFailOnError
End With
You probably can.I just wish I could have my number fields be blank instead of 0
You can if they are NULL ?OK. got it. Thank you all. I just wish I could have my number fields be blank instead of 0 but that's more of an aesthetic thing so no big deal. I can just format it on the form.
Private Sub MatchTransfer()
Dim strSql As String
Dim AID As Long
Dim NID As Long
Dim CDt As Date
Dim Nm As String
Dim Mmo As String
Dim TrnID As Long
Dim TPay As Currency
Dim TRec As Currency
Dim TMmo As String
Dim ID2 As Long 'New match
AID = Me.Parent.fAccountID
NID = Me.Parent.cboFullName.Value
CDt = Me.Parent.CkDate
Nm = Nz(Me.Parent.Num, "")
Mmo = Nz(Me.Parent.Memo, "")
TPay = Nz(Me.TPayment)
TRec = Nz(Me.TReceipt)
TMmo = Nz(Me.TranMemo, "")
'add matching record
strSql = StringFormatSQL("INSERT INTO tblTransactions(fAccountID, fNameID, CkDate, Num, Payment, Receipt, Memo)" & _
" VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6});", AID, NID, CDt, Nm, TPay, TRec, Mmo)
SQLExecute strSql
'add connector record
ID2 = DMax("TransactionID", "tblTransactions")
strSql = StringFormatSQL("INSERT INTO tblCheckTran(fTransactionID, TPayment, TReceipt, TranMemo)" & _
" VALUES ({0}, {1}, {2}, {3});", ID2, TPay, TRec, TMmo)
SQLExecute strSql
End Sub
Nm = Nz(Me.Parent.Num, "")
Nm = Me.Parent.Num
Dim Nm As String
Dim Nm As Variant
OK, but still I advise to use NULL instead of empty string!Num is actually a string. It's the currency fields that make trouble.
Private Sub MatchTransfer()
Dim strSql As String
Dim AID As Long
Dim NID As Long
Dim CDt As Date
Dim Nm As Variant
Dim Mmo As String
Dim TrnID As Long
Dim TPay As Variant
Dim TRec As Variant
Dim TMmo As String
Dim ID2 As Long 'New match
AID = Me.Parent.fAccountID
NID = Me.Parent.cboFullName.Value
CDt = Me.Parent.CkDate
Nm = Me.Parent.Num
Mmo = Nz(Me.Parent.Memo, "")
TPay = Me.TPayment
TRec = Me.TReceipt
TMmo = Nz(Me.TranMemo, "")
'add matching record
strSql = StringFormatSQL("INSERT INTO tblTransactions(fAccountID, fNameID, CkDate, Num, Payment, Receipt, Memo)" & _
" VALUES ({0}, {1}, {2}, {3}, {4}, {5}, {6});", AID, NID, CDt, Nm, TPay, TRec, Mmo)
SQLExecute strSql
'add connector record
ID2 = DMax("TransactionID", "tblTransactions")
strSql = StringFormatSQL("INSERT INTO tblCheckTran(fTransactionID, TPayment, TReceipt, TranMemo)" & _
" VALUES ({0}, {1}, {2}, {3});", ID2, TPay, TRec, TMmo)
SQLExecute strSql
End Sub
Great naming convention.Num is actually a string. It's the currency fields that make trouble.
No, a variant can be whatever is put in to it. I *think* the StringFormatSQL() function will be able to determine what the passed datatype is.So... a variant will still always be a number in this case?