Saving a SQL result that has multiple data being read from a Microsoft Access database - sql

I'm trying to send a email with multiple data from an SQL statement but I can't get the result of the SQL statement to be saved in the variable.
str = "SELECT Test.*,TestTopicScore.[Easy Question Score],TestTopicScore.[Medium Question Score],TestTopicScore.[Hard Question Score] from Test INNER JOIN TestTopicScore on Test.TestID = TestTopicScore.TestID ORDER BY Test.Score DESC"
mycommand = New OleDbCommand(str, myconnection)
myReader = mycommand.ExecuteReader
myReader.Read()
emailcontents = myReader("emailcontents")
The connection with the access database is fine and the data is read but i just cant get it to save in a variable.
The error I get:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll
Additional information: emailcontents

Let's look at your query first, I've formatted it a bit to make it easier to see what's going on:
SELECT Test.*
,TestTopicScore.[Easy Question Score]
,TestTopicScore.[Medium Question Score]
,TestTopicScore.[Hard Question Score]
FROM Test
INNER JOIN TestTopicScore
ON Test.TestID = TestTopicScore.TestID
ORDER BY Test.Score DESC
What that is doing is selecting several different fields, not one value. Your code emailcontents = myReader("emailcontents") attempts to read one field which is named "emailcontents". Unfortunately, there is no such named field.
It is worse than that: you don't even know how many fields Test.* is going to return.
So, start by enumerating every field which you want from Test.
Then, you appear to want just one string as the result, so you will need to concatenate the fields as strings instead of returning them separately.
In Access, the operator to join strings together is &, and it should convert numeric values into strings for you, thus:
SELECT Test.SomeField & Chr(13) & Chr(10)
& Test.SomeOtherField
& Test.AnotherField
& TestTopicScore.[Easy Question Score]
& TestTopicScore.[Medium Question Score]
& TestTopicScore.[Hard Question Score]
AS [emailcontents]
FROM Test
INNER JOIN TestTopicScore
ON Test.TestID = TestTopicScore.TestID
ORDER BY Test.Score DESC
Notice how I put & Chr(13) & Chr(10) between Test.SomeField and Test.SomeOtherField: that puts in a new line between the two. You might want to do it in between all the fields.
Also, I gave a name to the concatenated data with AS.
Now we're getting into really messy stuff by having to put the formatting of the output into the database. You don't want to do that. Let me emphasise this so it stands out: A better way is to read all the fields individually in your VB.NET code and format them into a string there.
ETA: Not only all that, but it looks like you might return more than one set of results (from seeing the ORDER BY), in which case you will have to read all the results in a loop.

Related

Date MS access vb.net

I want to update an item's expiration date in MS access but It throws a data type mismatch
This is the code I used to update the item please check this Thanks I tried doing addwithvalue and add("expD",oledbtype.Date) Non works
Query
query = "Update Medicines set BarcodeID = #BarcodeID, Drugs =#DrugName, Dosage = #Dosage, Quantity= #Quantity, Station =#Station, ExpD =#ExpDate, Price=#Price, stckid=#stckid, IsActive='" & ComboBox1.Text & "' where BarcodeID=#BarcodeID"
My parameters
cmd.CommandText = query
cmd.Parameters.AddWithValue("#BarcodeID", txtbarcode.Text)
cmd.Parameters.AddWithValue("#DrugName", txtdrugs.Text)
cmd.Parameters.AddWithValue("#Dosage", txtdosage.Text)
cmd.Parameters.AddWithValue("#Quantity", txtquantity.Text)
cmd.Parameters.AddWithValue("#Station", txtstation.Text)
cmd.Parameters.AddWithValue("#Price", txtprice.Text)
'cmd.Parameters.Add("#ExpD", OleDbType.Date)
'cmd.Parameters.AddWithValue("#ExpDate", dtExpD.Value.Date)
cmd.Parameters.Add("#ExpDate", OleDbType.Date).Value = Me.dtExpD.Value.Date
cmd.Parameters.AddWithValue("#stckid", txtstockid.Text)
cmd.Parameters.Add("#IsActive", OleDbType.LongVarChar)
cmd.Parameters("#IsActive").Value = combocontainer
Thanks
Even though you are using parameter names in your SQL code, the Jet and ACE OLE DB providers ignore those names and substitute values from parameters into the SQL code based on position. That means that you MUST add the parameters to the command in the same order as they appear in the SQL code. You ought to do that regardless but it is essential for Access databases. Your SQL code contains "ExpD =#ExpDate, Price=#Price" but you add parameters like this:
cmd.Parameters.AddWithValue("#Price", txtprice.Text)
cmd.Parameters.Add("#ExpDate", OleDbType.Date).Value = Me.dtExpD.Value.Date
The order is reversed so those values are actually getting used the wrong way around. At least you were alerted by the data type mismatch. If they had been the same type then it would have worked but saved the wrong data to the wrong columns.

Data adapter troubles

When I run the code the line which filled my datatable says that there is no value given for one or more parameters
Order = New OleDb.OleDbDataAdapter("SELECT * FROM Orders WHERE
Driver_ID = " & ID, DBREF)
Order.Fill(dataODtable)
DGVorders.DataSource = dataODtable
The code says this:
An unhandled exception of type 'System.Data.OleDb.OleDbException'
occurred in System.Data.dll
Below is an image link to the database and table it is referencing.
(Database orders table)
If I try run the code without the where statement it runs without crashing.
The field DRIVER_ID is clearly a string, as such you need single quotes around the value you want to use for the WHERE clause.
But that would be wrong for a long list of reasons (Sql Injection, Parsing errors, automatic type conversion with incompatible locales).
So you really need to start using parameterized queries as soon as possible to avoid these misteps
Dim cmdText = "SELECT * FROM Orders WHERE Driver_ID = #drvID"
Order = New OleDb.OleDbDataAdapter(cmdText, DBREF)
Order.SelectCommand.Parameters.Add("#drvID", OleDbType.VarWChar).Value = ID
Order.Fill(dataODtable)
DGVorders.DataSource = dataODtable
Now the query is no more built concatenating together strings pieces (the main source for sql injection hacks) but you create a parameter object of the correct type and pass it to the database engine that will use it when needed.
Another benefit is the more clear code you get. In this case perhaps is not very evident but with more complex queries you will have a more clear understanding of what you are asking to do to the database.
You can try like this:
Order = New OleDb.OleDbDataAdapter("SELECT * FROM Orders WHERE
Driver_ID = '"& ID &"'", DBREF)
Order.Fill(dataODtable)
DGVorders.DataSource = dataODtable
maybe this will be the problem.
if Driver_ID is alphanumeric column, select query should be SELECT * FROM Orders WHERE Driver_ID = '" & ID & "' ;"

Why doesn't my query use my criteria?

I have a db in Access and I'm trying to get a textbox to run my query and pass an other bounded textbox's value in as the criteria in DLookUp. I have the query running in design view and when I enter the criteria directly it returns the correct results. When I open the report it gives me the sum of all the possible rows. In other words it doesn't filter the rows.
I haven't used Access in about twelve years, thankfully, and everything I've done up to this point has been tutorial/example patchwork, but here it is...
SQL Query:
SELECT Sum(IIf(Attended=-1,1,0)) AS attendance
FROM Students_Classes_Attendance
WHERE (((CStr([Students_Classes_Attendance].[Class_Id]))=[classId]));
DLookUp as Control Source:
=DLookUp("[Total Attendance by Class]![attendance]",
"[Total Attendance by Class]",
"[Class_Id] =" & [Class_Id])
I'm lost at the moment. I'm guessing that the value isn't there before the query fires and since the criteria is an optional parameter that it's being passed null, but I would hope you'd get an error from that. Not that #Error is very meaningful anyway.
Does anyone know for certain the problem and the best way to correct it? Thanks.
Edit:
I did the changes recommended in the answer so now my DLookUp looks like...
=DLookUp("[attendance]",
"[Total Attendance by Class]",
"[Class_Id] =" & [Class_Id])
...still returns the total for all rows. Removing the criteria completely makes no difference either, which returns me to thinking it has something to do with the bound textbox not having a value.
DLookup uses the following syntax:
Syntax for numerical values:
DLookup("FieldName" , "TableName" , "Criteria = n")
Syntax for strings: (note the single apostrophe before and after the string value)
DLookup("FieldName" , "TableName" , "Criteria= 'string'")
Syntax for dates: (note the # before and after the date value)
DLookup("FieldName" , "TableName" , "Criteria= #date#")
I believe you just need to remove the table name from the first parameter. Try this:
=DLookUp("[attendance]", "[Total Attendance by Class]", "[Class_Id] = " & [Class_Id])
Keep in mind that if Class_Id is a Text Field, you need to surround it by single quotes:
=DLookUp("[attendance]", "[Total Attendance by Class]", "[Class_Id] = '" & [Class_Id] & "'")

How do I save the result of an SQL COUNT query with VBA in Access 2007?

I'm trying to count the number of records in a table that meet a certain criteria. My preference is to use SQL, not Dcount, as I want to get better at SQL. Here's my current code below:
Dim countString As String
Dim count
countString = "SELECT COUNT(*) FROM `Engagement Letters` WHERE 'Client ID' = " & Me.cboSelectClient
count = CurrentDb.OpenRecordset(countString).Fields(0).Value
Yeah I know, I've used spaces in my tables and field names - I will change that. Though I think I should still be able to run this query as is, so I will leave it as is for now.
When I run the above, I get runtime error 3464 - data type mismatch in criteria expression. I've had the below dcount function work fine:
count = DCount("[Engagement Letter ID]", "Engagement Letters", "[Client ID] = " & Me.cboSelectClient)
And also the below COUNT query without the WHERE works fine:
"SELECT COUNT(*) FROM `Engagement Letters`"
My knowledge of SQL is very minimal, and my knowledge of more advanced VBA is also quite minimal, so I'm not sure where I'm going wrong. Can anyone help me with this?
Try building your string like this.
countString = "SELECT COUNT(*) FROM [Engagement Letters]" & vbCrLf & _
"WHERE [Client ID] = " & Me.cboSelectClient
Debug.Print countString
Use square brackets around object (table and field) names which include spaces or any characters other than letters, digits, and the underscore character.
For the table name, you used `Engagement Letters`, and the backticks appear to work the same as square brackets. Perhaps they always work equally well, but I don't know for sure because I use the brackets exclusively. And brackets instead of backticks might help you avoid this mistake ...
WHERE 'Client ID' = " & Me.cboSelectClient
... that was asking the db engine to compare the literal string, "Client ID", to the numerical value (?) you pulled from cboSelectClient.
I used vbCrLf between the 2 parts of the SELECT statement because I find that convenient when examining the completed string (via Debug.Print).

dlookup multiple tables and set textbox to result access 2007

I'll try and break down my problem as best I can and explain what I'm trying to achieve. Firstly, I have three tables:
**RFI** (stands for Request For Information)-
Fields: rfi_id, Customer_id .....
**RFI_project** -
Fields: rfipro_id, project_id, rfi_id *"....." represents other unnecessary fields*
**Customer** -
Fields: Customer_id, company .....
I have an access form with two comboboxes. On the first combobox I select the name of a project at which point the second textbox changes to show those *rfi_id*'s where there is a match with the project name selected.
Now what I'm trying to do is this - When I select an *rfi_id* in the second combobox I want it to display in a textbox on my form the company where there the *rfi_id* value matches the value in the combobox. It's a bit tricky due to the way the tables are joined...here is what I'm essentially trying to display in the textbox field in SQL terms:
SELECT Customer.company, RFI.Customer_id
FROM Customer, RFI
WHERE (((Customer.Customer_id)=[RFI].[Customer_id]) AND ((RFI.rfi_id)=[Forms]![Request for Info Form]![Combo90]))
ORDER BY Customer.company;
In order to do this I have tried the following to no avail. In the after update event of my second combobox I have inserted the following:
companyTB = DLookup("company", "Customer", "Customer_id =" & DLookup("Customer_id", "RFI" And "rfi_id =" & [Forms]![Request for Info Form]![cmbRFI]))
When I change the combobox value I get the error Run-time error '13': Type mismatch. I've tried searching for what I've done wrong but this is a very broad error apparently and I can't find anything similar (or that I understand). I also tried this instead -
companyTB = DLookup("company", "Customer", "Customer_id =" & DLookup("Customer_id", "RFI", "rfi_id =" & cmbRFI))
which gives me the following error - Run-time error '3075': Syntax error(missing operator)in query expression. Anyway, would anybody be kind enough to give me a breakdown of what I need to do to achieve this, or what I'm doing wrong (or maybe a better way to do it?). Forgive me for being to seemingly stupid at this, I've only just begun working with access more in depth in the last 3 weeks or so. Thank you.
Your first DLookUp has incorrect syntax:
companyTB = DLookup("company", "Customer", "Customer_id ="
& DLookup("Customer_id", "RFI" And "rfi_id ="
& [Forms]![Request for Info Form]![cmbRFI]))
I have broken it into three lines to make this easier to see. The second line has And between "RFI" and "rfi_id" when it should have a comma.
companyTB = DLookup("company", "Customer", "Customer_id ="
& DLookup("Customer_id", "RFI", "rfi_id =" & cmbRFI))
The error you are getting on your second combo seems likely to be due to the result returned by cmbRFI. You can check this by filling in an actual rfi_id, rather than the reference to the combo and by setting a text box equal to cmbRFI and see what it is returning. Combo can be difficult because the displayed column and the bound column can be different.
It can be convenient to set up your combobox with several columns, as shown in your query, so the rowsource might be:
SELECT rfi.ID, Customer.company, RFI.Customer_id
FROM Customer
INNER JOIN RFI
ON Customer.Customer_id=RFI.Customer_id
ORDER BY Customer.company;
(or the three tables, joined, if necessary)
Then
Column count = 3
Column widths = 2cm;0;0
Bound column = 1
You can now refer to the second column in your textbox:
= cmbRFI.Column(1)
Columns are numbered from zero.
It is always worth reading up on sql before working with Access:
Fundamental Microsoft Jet SQL for Access 2000
Intermediate Microsoft Jet SQL for Access 2000
Advanced Microsoft Jet SQL for Access 2000
Your last Dlookup statement seems absolutely fine so I'm a little confused as to why it isn't working, you may be able to get round the problem like this though:
Combox2_AfterUpdate (Or whatever the event is called)
Dim rs As Recordset
Set rs = Currentdb.OpenRecordset("SELECT C.Company, R.Customer_ID " & _
"FROM Customer As C, RFI As R " & _
"WHERE C.Customer_ID = R.Customer_ID " & _
"AND R.RFI_ID =" & [Forms]![Request for Info Form]![Combo90] & " " & _
"ORDER BY C.Company")
CompanyTB = rs!Company
rs.Close
Set rs = Nothing
End Sub