Ms access query - sql

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 & "*';"

Related

SQL VBA syntax issue

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, "'", "''") & "%' "

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

Visual Basic update column is doing arthmetic in query

I have a page that displays results in a SQL database. And then I have another page that lets me edit whichever row I want to edit. One of the fields are dates. If added into the database through one of my pages it gets put in with the format (YEAR-MN-DY)(2014-04-11). When I go to UPDATE the date it then does arithmetic on the date. For example. If the date is currently 2014-04-11 and I update/change the date to 2010-01-01 it will replace the date with "2008" which is 2010 -1 - 1.
The variable is a string that is received through a HTML form.
strSQL = "UPDATE sales SET cust_id = " & intcust_id & ", agent_id = " & intagent_id & ", saledate = " & strsaledate & " WHERE sale_id = " & intsale_id & ""
Is the SQL query I am running.
Also, the DATE is VARCHAR2 in the database and used as a string throughout my VB code. I kept it this way because not everyone enters the date the same and this is for simplicity.
The subtraction is occurring because the date is not being interpreted as a date, but as a number because it is missing its quotes.
Before
strSQL = "UPDATE sales SET cust_id = " & intcust_id & ", agent_id = " & intagent_id & ", saledate = " & strsaledate & " WHERE sale_id = " & intsale_id & ""
After
strSQL = "UPDATE sales SET cust_id = " & intcust_id & ", agent_id = " & intagent_id & ", saledate ='" & strsaledate & "' WHERE sale_id = " & intsale_id & ""
The answer from WorkSmarter tackles the problem. I think however you should not use string concatenation. It is wide open to sql-injections and it is indeed much nicer, simpler and less error prone to use parameters. Something like
strSQL = "UPDATE sales SET cust_id = #custid, agent_id = #agentid, saledate = #salesdate WHERE sale_id = #saleid"
set custid = cmd.CreateParameter("#custid", adChar,adInput,10, incust_id)
set agentid = cmd.CreateParameter("#cagentid", adInteger,adInput,0, ) ...
I'm asuming you have an ADODB.Command by the name of cmd. By doing it like this you are making your code a lot safer and, in my opinion, more readable since you don't have to worry about quotes and single quotes all the time. There is a clear distinction between the sql command and the params/values involved.
You can find good documentation on parameter on http://www.w3schools.com/asp/met_comm_createparameter.asp

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'));"