SteppingHat
SteppingHat

Reputation: 1362

Access VBA simple sql select statement

Coming from years of web development where PHP and SQL statements were so simple, this recent task I've been required to undergo with MS Access and VBA is absolutely doing my head in at how much it complicates SQL statements. Mind you I have no prior knowledge about VBA so it could be extremely simple and I'm not just getting it, but all I want to do is

"SELECT type FROM tblMatter WHERE id='$id'"

When I wear my PHP cap, I want to think okay, we are going to have one row of data, that's going to be an array, and I want one object out of that array. Simple.

VBA, however, complicates the $#!t out of it. So far my code looks something like this

Dim matterSQL As String
Dim matterRS As Recordset

matterSQL = "SELECT type FROM tblMatter WHERE id'" & id & "'"
Set matterRS = CurrentDb.OpenRecordset(matterSQL)

MsgBox matterRS![type]

CurrentDb is defined much much earlier in the code to open the connection to the database, and the error is on the line containing OpenRecordset with the error: Data type mismatch in criteria expression.
As I said, I'm new to VBA so I don't know what the heck I'm doing, and all the documentation on the internet is nowhere near helpful. But all I want to do is to get one piece of data from the table.

Thanks in advance!


Edit: I needed to build upon this with another query that takes the info from the last query to run. Same kind of ordeal:

Dim costSQL As String
Dim costRS as Recordset

costSQL = "SELECT email FROM tblScaleOfDisb WHERE category=" & category
Set costRS = CurrentDb.OpenRecordset(costSQL)

MsgBox costRS![email]

This time I'm getting an error on the line containing OpenRecordset with the error: Too few parameters. Expected 1.
Which I don't understand because the code is practically the same as the first half of the question. What have I done wrong?

Upvotes: 1

Views: 12208

Answers (2)

Krish
Krish

Reputation: 5917

You must understand or prepare few things before you start coding on a new platform. such as

  1. Using Keywords/ Reserved keywords
  2. Capturing Errors
  3. basic arithmetic operations/ string operations.
  4. Available functions / methods
  5. Ways of cleaning your variables after using it

in your case you also need to learn about MS ACCESS SQL. Which is pretty similar to standard SQL but (limited to and) strongly influenced by MS Access internal functions.

SQL execution will return n Rows as result. Each row will have n number of columns. You need to understand how you need to loop through result sets.

Please do have some error capturing method. I will help you to understand the direction before spending hours in Google.

in your first SQL: you have a reserved keyword Type. use square brackets to escape reserved keywords. In where condition numeric fields must not have string quotes and strings must have them.

Tip: You can use the MS Access visual query builder to build your query and copy the SQL to VBA.

list of reserved keywords in MS ACCESS: https://support.microsoft.com/en-us/kb/286335

list of functions: http://www.techonthenet.com/access/functions/

Error handling : http://allenbrowne.com/ser-23a.html

Clean/close your objects after usage by explicitly setting as nothing: Is there a need to set Objects to Nothing inside VBA Functions

Upvotes: 0

Sachu
Sachu

Reputation: 7766

You are missing = in the condition

try below

matterSQL = "SELECT type FROM tblMatter WHERE id='" & id & "'"

Also if id is numeric you don't need '

matterSQL = "SELECT type FROM tblMatter WHERE id=" & id 

Too few parameters. Expected 1. This happens when the field name in your sql query do not match the table field name if the field name are correct i believe the the datatype of category is not numeric then you have to use '

costSQL = "SELECT email FROM tblScaleOfDisb WHERE category='" & category &"'"

Always try to use parameterised query to avoid SQL injection

Upvotes: 2

Related Questions