SQL VBA syntax issue - sql

This select statement works.
rs.Open "Select Company from Customers where Company LIKE '" & Replace(Range("K4").Value, "'", "''") & "%' "
This select statement doesn't work.
rs.Open "Select Company from Customers where '" & Range("N4").Value & "' LIKE '" & Replace(Range("K4").Value, "'", "''") & "%' "
I'm going to have a data validation drop down box where the user can pick between a few options so being able to change the second Company in the first select statement to the users selection would be handy. Something is wrong with the syntax because although it doesn't give me any errors it doesn't have any results.

You shouldn't quote the column name - notice there are no quotes in your first example.
rs.Open "Select Company from Customers where " & Range("N4").Value & _
" LIKE '" & Replace(Range("K4").Value, "'", "''") & "%' "

Related

Move an MS Access query into a VB6 query that uses the first

I have two tables, one table called Act_Reg and the other is Active_Pay. I have two queries: one query is a view in MS Access and it gives me the result as Paycoach :
SELECT Act_Reg.member_id, Active_Pay.date_pay, Active_Pay.kind_sport,
Active_Pay.kind_prac, Active_Pay.coach, Active_Pay.tuition, Active_Pay.discount
FROM Act_Reg
INNER JOIN Active_Pay ON Act_Reg.member_id = Active_Pay.member_id;
The second query I use in VB6 for getting the result of query one:
rstemp1.Open "SELECT sum(tuition)-sum(discount) FROM paycoach where date_pay Between '" & Trim(txtdatein.text) & "' And '" & Trim(txtdateto.text) & "' and coach='" & Cbocoach.text & "' and kind_sport='" & cbosport.text & "' and kind_prac='normal' group by tuition", db, adOpenKeyset, adLockOptimistic
I want to calculate the payment for each coach, according to paid membership and discount in a date range. The two queries work well, but one query is in MS Access as view and the second is in VB6.
How can I combine these two queries into one query, which I can use in Visual Basic 6?
I think you can do just what you ask by doing exactly how you describe it.
It's probably not the most efficient, but this should work by simply inserting your Access query SQL directly into your VB6 query SQL
your paycheck query SQL
SELECT Act_Reg.member_id, Active_Pay.date_pay, Active_Pay.kind_sport,
Active_Pay.kind_prac, Active_Pay.coach, Active_Pay.tuition, Active_Pay.discount
FROM Act_Reg
INNER JOIN Active_Pay ON Act_Reg.member_id = Active_Pay.member_id;
Your rs.Open sql
"SELECT sum(tuition)-sum(discount) FROM paycoach where date_pay Between '" & _
Trim(txtdatein.text) & "' And '" & Trim(txtdateto.text) & "' and coach='" & _
Cbocoach.text & "' and kind_sport='" & cbosport.text & "' and kind_prac='normal' group by tuition"
Modify your rs.open to use Combined statement
Dim sql as string
sql = "SELECT sum(tuition)-sum(discount) FROM "
sql = sql & "(SELECT Act_Reg.member_id, Active_Pay.date_pay, Active_Pay.kind_sport, "
sql = sql & "Active_Pay.kind_prac, Active_Pay.coach, Active_Pay.tuition, Active_Pay.discount "
sql = sql & "FROM Act_Reg INNER JOIN Active_Pay ON Act_Reg.member_id = Active_Pay.member_id) "
sql = sql & "As paycoach "
sql = sql & "where date_pay Between '"
sql = sql & Trim(txtdatein.text) & "' And '" & Trim(txtdateto.text) & "' and coach='"
sql = sql & Cbocoach.text & "' and kind_sport='" & cbosport.text & "' and kind_prac='normal' group by tuition"
rstemp1.Open sql, db, adOpenKeyset, adLockOptimistic

ASP Classic SQL Query does not work as intended

I am using ASP within HTML to create a website.
On one of my pages the SQL query sqlString = "SELECT * FROM Property_Details WHERE " &_
" Price BETWEEN '" & minPrice & "' AND '" & maxPrice & "' " &_
"OR Address_2 LIKE '" & searchFor & "' "
is used to search a database and display the correct entries based on what was on a form. This works for the Address_2 part of the query but the BETWEEN is not working correctly.
minPrice and maxPrice are all declared earlier and the correct form data is being taken as I have tested it with <%= minPrice %> what am I missing?
I think your minPrice and maxPrice are numbers, so you don't need apostrophe. Currently with your query you are getting SQL similar to this:
SELECT * FROM Property_Details
WHERE Price BETWEEN '10' AND '20'
Since 10 and 20 are numbers, so you should not use ' around them. Try changing your query to the following:
sqlString = "SELECT * FROM Property_Details WHERE " &_
" Price BETWEEN " & minPrice & " AND " & maxPrice & " " &_
"OR Address_2 LIKE '" & searchFor & "' "
Just as a side note - I don't know where minPrice, maxPrice and searchFor are coming from, but if they come from user input you could be vulnerable to SQL injection attacks

ACCESS VBA using using one more SQL QUERY in WHERE clause

I have two tables, one is std Table and KeyTable.
I have to display the values of std table based on the condition of keytable.
For example, if the user selects ASIA, i have to create a sql query list of countries in ASIA and use this country values (example: INDIA,SRILANKA,BANGLADESH) as where clause in final query. I have tried building them. But I am not sure of the syntax
First query to filter out the values of ASIA
strSQL1 = "SELECT keytable.[Lead Country] FROM keytable WHERE keytable.country='" & lstcountry & "';"
Second query to display the values according to the first query in where clause
strSQL = "SELECT * FROM [Std Table] WHERE ([Std Table].Country IN (______));
The blank which i am missing.
You could just combine the two queries into one statement:
strSQL= "SELECT * " & _
"FROM [Std Table] " & _
"WHERE [Std Table].Country IN " & _
"(" & _
" SELECT s.[Lead Country] " & _
" FROM keytable AS s WHERE s.country = '" & lstcountry & "' " & _
")"
I'd try
strSQL1 = "SELECT keytable.[Lead Country] FROM keytable WHERE keytable.country='" & lstcountry & "'"
strSQL = "SELECT * FROM [Std Table] WHERE ([Std Table].Country IN (" & strSQL1 & "));"
SQL Server would allow you to do that, I guess that ACCESS supports this, too.
Please note that I removed the ; in strSQL1!
You could also provide a list of comma separated values:
strSQL = "SELECT * FROM [Std Table] WHERE ([Std Table].Country IN ('DE','UK','JP'));"

Ms access query

My MS-Access query is
strSQL = "Select pincode from pincodes WHERE officename = '" &area& "' AND Districtname = '" &city& "';"
but when I run this query it shows record not found even the value that are passing in area and city exist in ms access table
I try the code as
strSQL = "Select pincode from pincodes WHERE officename = '" &area& "';"
and it give correct result but I want to verify the city too but I found that two checks not perform when query for both column how to resolve the problem I am attaching database shot from where it is retrieving value please help
snapshot here
Debug.
strSQL = "Select pincode from pincodes WHERE officename = '" &area& "' AND Districtname = '" &city& "';"
Response.write strSQL
Response.end
copy the output of the response.write and run it directly in the Db and see if you get any values.
The problem seems to be in the data. there maybe blank spaces or something like that. Try running the query with LIKE operator:
strSQL = "Select pincode from pincodes WHERE officename = '" & area & "' AND Districtname LIKE '*" & city & "*';"

Syntax error creating an SQL query dynamically

I'm using VB6 and MS Access. my sql command is
insert into BatchInfo (BName,BDate,Currency) values('" & Me.txtBatchName.Text & "','" & Me.dtpBatchDate.Value & "','" & Me.cboCurrency.Text & "')
the output of the command at run time is
"insert into BatchInfo (BName,BDate,Currency) values('batch1','8/2/2012','AED')"
here is the schema of the BatchInfo Table
BatchID AutoNumber
BName Text
BDate Date/Time
Currency Text
I cannot find any syntax error. Please help.
Currency is a reserved word, escape it thusly;
insert into BatchInfo (BName, BDate, [Currency]) values (...
MS Access typically likes # signs around its dates:
insert into BatchInfo (BName,BDate,Currency)
values('" & Me.txtBatchName.Text & "','#" & Me.dtpBatchDate.Value & "#','" & Me.cboCurrency.Text & "')
You are using single quotes, not double quotes.
Try this
insert into BatchInfo (BName,BDate,Currency) values(""" & Me.txtBatchName.Text & """,#" & Me.dtpBatchDate.Value & #",""" & Me.cboCurrency.Text & """)