Selecting data based on multiple fields (Where And) - sql

I have large amounts of data that I am trying to sort out based on two cascading combo boxes. I get error Microsoft Access can't find the field '|1' referred to in your expression and it points me to:
ElseIf [Forms]![Send To GE]![cboFil] = "LCP" Then
strSQL = "Select * From [To_GE] Where [Community] = " & Chr(34) & Me.cboSubFil.Value & Chr(34) And [LCP] = "& Chr(34) & Me.cboSSubFil.Value & Chr(34)"
Set rst = db.OpenRecordset(strSQL)
It seems that the And should work for this. What's causing this error, and how can I resolve it?

strSQL = "Select * From [To_GE] Where [Community] = " & Chr(34) & Me.cboSubFil.Value & Chr(34) And [LCP] = "& Chr(34) & Me.cboSSubFil.Value & Chr(34)"
Should maybe be
strSQL = "Select * From [To_GE] Where [Community] = " & Chr(34) & Me.cboSubFil.Value & Chr(34) & " And [LCP] = " & Chr(34) & Me.cboSSubFil.Value & Chr(34)
To make things read a little easier though I would recommend escaping your quotes or switching to single quotes in the query
strSQL = "Select * From [To_GE] Where [Community] = '" & Me.cboSubFil.Value & "' And [LCP] = '" & Me.cboSSubFil.Value & "'"

Related

Run-time error ‘3061’: Too few parameters expected 1

i had this error when i was trying to INSERT records to a local table with VBA.
I have checked the data type and putting in the quotes for the the short text data type but it doesn't work.
table_newid = "SELECT Cint(t1." & id_name(i) & "_new), " & Replace(select_column_str, local_table_name, "t2") & " FROM " & vbCrLf & _
"(SELECT CInt(DCount(""[" & id_name(i) & "]"", """ & qry_distinct_id_name & """, ""[" & id_name(i) & "]<="" & [" & id_name(i) & "])) as " & id_name(i) & "_new, * FROM " & qry_distinct_id_name & ") AS t1 " & vbCrLf & _
"LEFT JOIN " & local_table_name & "_ALL as t2 " & vbCrLf & _
"ON t1." & id_name(i) & " = t2." & id_name(i) & " " & vbCrLf & _
"WHERE t2.database = '" & database_name & "'"
strQuery = "INSERT INTO " & local_table_name & "_temp (" & temp_field(i) & ", " & Replace(select_column_str, local_table_name & ".", "") & ") " & vbCrLf & table_newid
Debug.Print strQuery
DoCmd.SetWarnings False
db.Execute strQuery
DoCmd.SetWarnings True
From the debug.print, i have got:
INSERT INTO TblLUMachineTypes_temp (MachTypeID_new, MachTypeID, MachTypeCode, MachTypeMod, MachTypeDesc, MachTypeDisc, NewCode, Approved, mttime, CreatedBy, CreatedTS, ModifiedBy, ModifiedTS)
SELECT t1.MachTypeID_new, t2.MachTypeID, t2.MachTypeCode, t2.MachTypeMod, t2.MachTypeDesc, t2.MachTypeDisc, t2.NewCode, t2.Approved, t2.mttime, t2.CreatedBy, t2.CreatedTS, t2.ModifiedBy, t2.ModifiedTS FROM
(SELECT CInt(DCount("[MachTypeID]", "qry_TblLUMachineTypes_id_distinct", "[MachTypeID]<=" & [MachTypeID])) as MachTypeID_new, * FROM qry_TblLUMachineTypes_id_distinct) AS t1
LEFT JOIN TblLUMachineTypes_ALL as t2
ON t1.MachTypeID = t2.MachTypeID
WHERE t2.database = 'CPM-252-2'
When i copied this query and execute it manually, it works fine but not with VBA. Any idea?
Thanks in advance.
Remove all the & vbCrLf from your code, they are not necessary and I assume they corrupt the SQL syntax.
I found the problem. I found that qry_distinct_id_name query table has a Dlookup function in there that returns a string value, which will work when executing the query manual but doesn't work when you run it with VBA. So I have re-written the code to put in the quote before and after dlookup() function.

When I write code inside textbox doesn't accept it

I have a textbox in a form. I use this textbox to write "Codes" and then I save it in the table in the database through the SQL insert statement, but the code doesn't accept to run and gives me an error message:
Run-Time error '3075'.
type of database: Access database
type of field data: LongText
What the problem and how to pass all same problems when I need to save codes inside the database field.
When I try to save the code without (') it's working!
I use this SQL Statement:
CurrentDb.Execute "Update Tbl_Codes Set [LP_ID]= " & Me.txtID & ",
[Code_Title]='" & Me.txtTitle & "'" _
& " ,[Code_Des]= '" & Me.txtDes & "',[Code_Key]= '" & Me.txtKey & "',
[Notes]= '" & Me.txtNotes & "'" _
& " Where [ID]= " & Me.txtID1 & ""
And I want to save this Code:
DSum("Field1";"Table";"Field2= '" & Value & "'")
Please change your code as follows. You need to escape single quotes by doubling them up. A simple replace will work for your.
CurrentDb.Execute "Update Tbl_Codes Set [LP_ID]= " & Replace(Me.txtID,"'","''") & ",
[Code_Title]='" & Replace(Me.txtTitle,"'","''") & "'" _
& " ,[Code_Des]= '" & Replace(Me.txtDes,"'","''") & "',[Code_Key]= '" & Replace(Me.txtKey,"'","''") & "',
[Notes]= '" & Replace(Me.txtNotes,"'","''") & "'" _
& " Where [ID]= " & Me.txtID1 & ""
DSum("Field1";"Table";"Field2= '" & Replace(Value,"'","''") & "'")

Using NZ in a function in VBA to build query

I am really struggling here. I am building up a query inside VBA to link to several tables inside Oracle and inside Access. I need to make sure what I have uploaded to Oracle Matches what is in the Access DB.
I have:
SourceField1 (Always populated)
SourceField2 (Sometimes populated)
If source field 2 is blank I want to ignore it and not join to it. The best way I have done this is try with an Nz and replace with SourceField1.
strSQL = "INSERT INTO ERROR_TABLE (ORACLE_FIELD, TRANSFORM_FIELD) SELECT " & MatchValues!ORACLE_TABLE_NAME & "." & MatchValues!FieldName & ", " & MatchValues!TRANSFORM_TABLE_NAME & "." & MatchValues!FieldName
strSQL = strSQL & " FROM " & MatchValues!TRANSFORM_TABLE_NAME & " INNER JOIN " & MatchValues!xfTableName
strSQL = strSQL & " ON " & MatchValues!TRANSFORM_TABLE_NAME & "." & MatchValues!SourceField1 & " = " & MatchValues!xfTableName & "." & MatchValues!ReferenceField1 & ""
strSQL = strSQL & " AND " & MatchValues!TRANSFORM_TABLE_NAME & ".Nz(" & MatchValues!SourceField2 & "," & MatchValues!SourceField1 & ") = " & MatchValues!xfTableName & ".Nz(" & MatchValues!ReferenceField2 & "," & MatchValues!ReferenceField1 & ")"
strSQL = strSQL & " INNER JOIN " & MatchValues!ORACLE_TABLE_NAME & " ON (" & MatchValues!xfTableName
strSQL = strSQL & ".KEYVAL = " & MatchValues!ORACLE_TABLE_NAME & ".KEYVAL)"
strSQL = strSQL & " WHERE (" & MatchValues!TRANSFORM_TABLE_NAME & "." & MatchValues!FieldName
strSQL = strSQL & " <> " & MatchValues!ORACLE_TABLE_NAME & "." & MatchValues!FieldName & ")"
Which gives me this:
INSERT INTO ERROR_TABLE (ORACLE_FIELD, TRANSFORM_FIELD) SELECT UNI73MART1_DCappl.DECSN, tbluniDCappl.DECSN FROM tbluniDCappl INNER JOIN XF_DC_ref ON tbluniDCappl.REFVAL = XF_DC_ref.REFVAL AND tbluniDCappl.Nz(,REFVAL) = XF_DC_ref.Nz(,REFVAL) INNER JOIN UNI73MART1_DCappl ON (XF_DC_ref.KEYVAL = UNI73MART1_DCappl.KEYVAL) WHERE (tbluniDCappl.DECSN <> UNI73MART1_DCappl.DECSN)
Try this with proper concatenation of the Nz expressions:
strSQL = "INSERT INTO ERROR_TABLE (ORACLE_FIELD, TRANSFORM_FIELD) SELECT " & MatchValues!ORACLE_TABLE_NAME & "." & MatchValues!FieldName & ", " & MatchValues!TRANSFORM_TABLE_NAME & "." & MatchValues!FieldName
strSQL = strSQL & " FROM " & MatchValues!TRANSFORM_TABLE_NAME & " INNER JOIN " & MatchValues!xfTableName
strSQL = strSQL & " ON " & MatchValues!TRANSFORM_TABLE_NAME & "." & MatchValues!SourceField1 & " = " & MatchValues!xfTableName & "." & MatchValues!ReferenceField1 & ""
strSQL = strSQL & " AND " & MatchValues!TRANSFORM_TABLE_NAME & "." & Nz(MatchValues!SourceField2, MatchValues!SourceField1) & " = " & MatchValues!xfTableName & "." & Nz(MatchValues!ReferenceField2, MatchValues!ReferenceField1) & ")"
strSQL = strSQL & " INNER JOIN " & MatchValues!ORACLE_TABLE_NAME & " ON (" & MatchValues!xfTableName
strSQL = strSQL & ".KEYVAL = " & MatchValues!ORACLE_TABLE_NAME & ".KEYVAL)"
strSQL = strSQL & " WHERE (" & MatchValues!TRANSFORM_TABLE_NAME & "." & MatchValues!FieldName
strSQL = strSQL & " <> " & MatchValues!ORACLE_TABLE_NAME & "." & MatchValues!FieldName & ")"
Pull the call to Nz out of the SQL. The two fields in question exist in the VBA context and so should be checked within the VBA code, not embedded in the SQL string.
strSQL = strSQL & " AND " & MatchValues!TRANSFORM_TABLE_NAME & "." Nz( MatchValues!SourceField2 , MatchValues!SourceField1) & " = " & MatchValues!xfTableName & "." & Nz( MatchValues!ReferenceField2 , MatchValues!ReferenceField1)
When NULL is concatenated with a string using &, null is just converted to a blank string. So the expression ".Nz(" & MatchValues!SourceField2 & "," & MatchValues!SourceField1 & ") = " & MatchValues!xfTableName & ".Nz(" & MatchValues!ReferenceField2 & "," & MatchValues!ReferenceField1 & ")" would simply produce the string value ".Nz(, SourceField1Name) = tablename.Nz(,ReferenceField1Name)... which is bad SQL syntax. I think Nathan_Sav was trying to point this out, but not very clear about it.
Hint 1: You should always debug an SQL statement built in code by printing out the actual SQL statement. You should have included that in your question.
Hint 2: Try using the VBA line continuation character _

Missing comma error error inserting sql through access 2010 vba

Having the weirdest problem ever. I do have an sql insert statement, that properly works in sql. When i put that sql into vba it works very good from my pc. However, it does not work and shows sql error: missing comma. Where can be the problem??? I use Access 2010 plus, others use same Access version and having same ODB connections (DSN servers) . Some code example:
sql = "Driver={Microsoft ODBC for Oracle}; " & _
"CONNECTSTRING=(DESCRIPTION=" & _
"(ADDRESS=(PROTOCOL=TCP)" & _
"(HOST= ODB)(PORT=1520))" & _
"(CONNECT_DATA=(SERVICE_NAME=ABTL))); uid=IN; pwd=XXX;"
Set con = New ADODB.Connection
Set rec = New ADODB.Recordset
Set cmd = New ADODB.Command
con.Open sql
Set db = CurrentDb()
Set rst = db.OpenRecordset(strSQL)
Do While Not rst.EOF
insetSQL = con.Execute(" INSERT INTO STOCK (PNM_AUTO_KEY, PN, DESCRIPTION, HISTORICAL_FLAG, qty_oh, qty_adj, qty_available, CTRL_NUMBER, CTRL_ID, receiver_number, rec_date, serial_number,pcc_auto_key, cnc_auto_key, loc_auto_key, whs_auto_key, unit_cost, adj_cost, stc_auto_key, visible_mkt, remarks, ifc_auto_key, exp_date, unit_price, tagged_by, tag_date, owner, PART_CERT_NUMBER,ORIGINAL_PO_NUMBER, SHELF_LIFE, CTS_AUTO_KEY, MFG_LOT_NUM, AIRWAY_BILL )" & _
" VALUES ( " & rst![minimumas] & ", '" & rst![pn] & "', '" & "FROM_SCRIPT" & "', '" & "F" & "'," & rst![qty_oh] & "," & rst![qty_oh] & "," & rst![qty_oh] & "," & "G_STM_CTRL_NUMBER.nextval" & "," & "1" & ", '" & rst![receiver_number] & "', " & " TO_DATE('" & rst![rec_date] & "','YYYY-MM-DD'), '" & rst![serial_number] & "'," & rst![pcc_auto_key] & "," & rst![cnc_auto_key] & "," & rst![loc_auto_key] & "," & rst![whs_auto_key] & "," & rst![unit_cost] & "," & rst![unit_cost] & "," & "1" & ",'" & "T" & "', '" & rst![remarks] & "'," & _
"1" & ", " & " TO_DATE('" & rst![exp_date] & "','YYYY-MM-DD'), " & "0" & ",'" & rst![TAGGED_BY] & "', " & " TO_DATE('" & rst![tag_date] & "','YYYY-MM-DD'), '" & "" & "', '" & rst![PART_CERT_NUMBER] & "', '" & rst![ORIGINAL_PO_NUMBER] & "', '" & rst![SHELF_LIFE] & "', " & rst![CERT_SOURCE] & ", '" & rst![MFG_LOT_NUM] & "', '" & rst![AIRWAY_BILL] & "'" & " )")
Loop
It might be that one of the rescordset fields that is a string data type, has a value that contains a comma but you have not treated it as string.
eg
VALUES ( " & rst![minimumas] & ",
For the above code to work minimumas CANNOT contain any commas.
Are you certain that all the string fields int he recordset have been appended to the SQL string with a ' around them.
We need to see the sql string is created which then causes the error. Please provide this as requested.
=========================================================
PART 2 Viewing the SQL causing the error
replace
insetSQL = con.Execute(
with
on error resume next
strMySQl = the string currently used in the execute statement
insetSQL = con.Execute(strMySQl)
if err.number <> 0 then
stop
debug.print err.number, err.description
debug.print strMySQl
end if
then post the sql string that is printed in the immediate window to this site.
I would also suggest that you compare the SQL string generated on your machine with the other machines - just in case something weird is happening.

SQL UPDATE Statement in excel VBA not working

I have this code to update database with SQL but it is not working
Call Connect_to_db
strSQL = "UPDATE StockTable " & _
"SET StockTable.Selected = '" & Sheets("InfoStockDes").Range("g" & x) & "' " & _
"WHERE OwnerName = '" & Sheets("InfoStockDes").Range("a" & x) & WHERE OwnerShipMethod = Sheets("InfoStockDes").Range("b" & g) & WHERE StockName = Sheets("InfoStockDes").Range("c" & g) & WHERE Quantity = Sheets("InfoStockDes").Range("d" & g) "' "
cn.Execute strSQL
Call Close_db
This is your code as posted, but then with some extra linebreaks and outlining. Maybe you can spot the obvious errors yourself better now?
Call Connect_to_db
strSQL = "UPDATE StockTable " & _
"SET StockTable.Selected = '" & Sheets("InfoStockDes").Range("g" & x) & "' " & _
"WHERE OwnerName = '" & Sheets("InfoStockDes").Range("a" & x) & _
WHERE OwnerShipMethod = Sheets("InfoStockDes").Range("b" & g) & _
WHERE StockName = Sheets("InfoStockDes").Range("c" & g) & _
WHERE Quantity = Sheets("InfoStockDes").Range("d" & g) "' "
cn.Execute strSQL
Call Close_db
In this version I have changed the extra WHERE clauses to AND, and I haev added some obviously missing " and '.
Call Connect_to_db
strSQL = "UPDATE StockTable " & _
"SET StockTable.Selected = '" & Sheets("InfoStockDes").Range("g" & x) & "' " & _
"WHERE OwnerName = '" & Sheets("InfoStockDes").Range("a" & x) & "' " & _
"AND OwnerShipMethod = '" & Sheets("InfoStockDes").Range("b" & g) & "' " & _
"AND StockName = '" & Sheets("InfoStockDes").Range("c" & g) & "' " & _
"AND Quantity = '" & Sheets("InfoStockDes").Range("d" & g) & "' "
cn.Execute strSQL
Call Close_db
I guess this shoudl work better allready, if it doesn't please tell us what is not working, what error messages, if any, you are getting, and please show the actual content of strSQL before yuo execute it. That string should contain a straightforward SQL statement, but there might be errors in it, so have a look at that and, if necessary, post it for us to look at :)