I am using following query to add date into ms access database. The format of DATEANDTIME is Date/Time in the ms access file
strSQL = "INSERT INTO Table1 (DATEANDTIME) values ('" & DateTimePicker1.Value.ToShortDateString & "')"
accessCommand.CommandText = strSQL
accessCommand.Connection = accessConnection
accessCommand.ExecuteNonQuery()
accessCommand.Dispose()
accessDataReader.Close()
MsgBox("OK Done")
Access file snapshot
enter image description here
Now i am using following query to fetch data from ms access database to ListViewLOCATION in vb.net
strSQL = "SELECT * from Table1 WHERE DATEANDTIME>='" & DateTimePicker1.Value.ToShortDateString & "' and DATEANDTIME <='" & DateTimePicker2.Value.ToShortDateString & "'"
accessCommand.CommandText = strSQL
accessCommand.Connection = accessConnection
accessAdopter.SelectCommand = accessCommand
accessDataReader = accessCommand.ExecuteReader
ListViewLOCATION.Items.Clear()
While (accessDataReader.Read())
ListViewLOCATION.Items.Add(accessDataReader("DATEANDTIME"))
End While
accessCommand.Dispose()
accessDataReader.Close()
but showing this error
Additional information: Data type mismatch in criteria expression.
Please help me to resolve this error
DateTime in Access is not text.
So, use a formatted text expression for the date value:
strSQL = "INSERT INTO Table1 (DATEANDTIME) Values (#" & DateTimePicker1.Value.ToString("yyyy'/'MM'/'dd") & "#)"
Likewise for the comparison/filtering.
Related
I am trying to use an update statement to update a certain record, however I am having difficulty. I would like to replace ‘user’ and ‘timestamp’ with variable values however When I provide inline variables I receive an error, can someone help me to achieve this functionality. I an using sql server as my dbms.
Variables:
Dim timeStamp As String = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")
Dim user As String = GetUserName()
Update Statement:
strsql = strsql & " Update Activity Set userName = 'user', timeDate = 'timeStamp' Where noteKey = " & lngNoteKey
You will want to use parameterized queries to do this.
strsql = strsql & " Update Activity Set userName = #user, timeDate = #date Where noteKey = " & lngNoteKey
and then add the parameters to the command when you execute it:
yourQuery.Parameters.Add(new ObjectParameter("user", user));
yourQuery.Parameters.Add(new ObjectParameter("date", timeStamp));
strsql = strsql & " Update Activity Set userName = '" & user & "', timeDate = '" & timeStamp & "' Where noteKey = " & lngNoteKey
Try that. concatenate the data into the string.
I am trying to add data from an Access form to a table. When I ran sql code, I got an error message saying "number of query values and destination fields are not the same."
this is my code:
Private Sub CmdAddtoProductionDetails_Click()
Dim StrSql As String
StrSql = "Insert Into test1 (ProductName, [Lot Number], ProductionDate, Quantity, Unit, Client) Values(me! ComboProduct1, me! LotNoProduct1, me! txtDate, me! Product1Quantity! me!ComboProduct1Unit, me! ComboProduct1Client)"
CurrentDb.Execute (StrSql)
End Sub
A simpler and more direct method is to use a recordset:
Private Sub CmdAddtoProductionDetails_Click()
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("Select Top 1 * From test1")
rs.AddNew
rs!ProductName.Value = Me!ComboProduct1.Value
rs![Lot Number].Value = Me!LotNoProduct1.Value
rs!ProductionDate.Value = Me!txtDate.Value
rs!Quantity.Value = Me!Product1Quantity.Value
rs!Unit.Value = Me!ComboProduct1Unit.Value
rs!Client.Value = Me!ComboProduct1Client.Value
rs.Update
rs.Close
Set rs = Nothing
End Sub
Your SQL string will be passed to the SQL engine which does not know how to interpret me!ComboProduct1 etc. You need to insert the values of those variables into the string:
Private Sub CmdAddtoProductionDetails_Click()
Dim StrSql As String StrSql = "Insert Into test1 (ProductName, [Lot Number], ProductionDate, Quantity, Unit, Client)" & _
" Values( '" & me! ComboProduct1 & "', '" & me! LotNoProduct1 & "', #" & Format(me! txtDate, "yyyy/mm/dd") & "#, " & CStr(me! Product1Quantity) & ", '" & me!ComboProduct1Unit & "', '" & me! ComboProduct1Client & "' )"
CurrentDb.Execute (StrSql)
End Sub
Put single quotes around strings but not around numbers. Some of your fields I wasn't sure if they were numbers or strings - I made a guess.
You need to be careful with dates - check that the SQL engine is interpreting dates in yyyy/mm/dd format correctly. It will convert the string #2016/06/04# to a date automatically for you.
All,
I am running the below SQL and I keep getting error 3061. Thank you all for the wonderful help! I've been trying to teach myself and I am 10 days in and oh my I am in for a treat!
Private Sub b_Update_Click()
Dim db As DAO.Database
Set db = CurrentDb
strSQL = "UPDATE Main" _
& " SET t_Name = Me.txt_Name, t_Date = Me.txt_Date, t_ContactID = Me.txt_Contact, t_Score = Me.txt_Score, t_Comments = Me.txt_Comments" _
& " WHERE RecordID = Me.lbl_RecordID.Caption"
CurrentDb.Execute strSQL
I am not sure but, you can try somethink like that
if you knom the new value to insert in the database try with a syntax like this one
UPDATE table
SET Users.name = 'NewName',
Users.address = 'MyNewAdresse'
WHERE Users.id_User = 10;
Now, if you want to use a form (php)
You have to use this
if(isset($_REQUEST["id_user" ])) {$id_user = $_REQUEST["id_user" ];}
else {$id_user = "" ;}
if(isset($_REQUEST["name" ])) {$name= $_REQUEST["name" ];}
else {$name = "" ;}
if(isset($_REQUEST["address" ])) {$address= $_REQUEST["adress" ];}
else {$adress= "" ;}
if you use mysql
UPDATE table
SET Users.name = '$name',
Users.address = '$adress'
WHERE Users.id_User = 10;
i don't know VBA but I will try to help you
Going on from my comment, you first need to declare strSQL as a string variable.
Where your error expects 6 values and access doesn't know what they are. This is because form objects need to be outside the quotations of the SQL query, otherwise (as in this case) it will think they are variables and obviously undefined. The 6 expected are the 5 form fields plus 'strSQL'.
Private Sub b_Update_Click()
Dim db As DAO.Database
dim strSQL as string
Set db = CurrentDb
strSQL = "UPDATE Main" & _
" SET t_Name = '" & Me.txt_Name & "'," & _
" t_Date =#" & Me.txt_Date & "#," & _
" t_ContactID =" & Me.txt_Contact & "," & _
" t_Score =" & Me.txt_Score & "," & _
" t_Comments = '" & Me.txt_Comments & "'," & _
" WHERE RecordID = '" & Me.lbl_RecordID.Caption & "';"
CurrentDb.Execute strSQL
end sub
Note how I have used double quotes to put the form fields outside of the query string so access knows they aren't variables.
If your field is a string, it needs encapsulating in single quotes like so 'string'. If you have a date field it needs encapsulating in number signs like so #date# and numbers/integers don't need encapsulating.
Look at the code I have done and you can see I have used these single quotes and number signs to encapsulate certain fields. I guessed based on the names of the fields like ID's as numbers. I may have got some wrong so alter where applicable... Or comment and I will correct my answer.
I have the following code:
sSQL = "SELECT CODER FROM " & dbfname & " IN " & dir & " WHERE TRIM(CODEK) = TRIM(kCode)"
Combo29.RowSource = sSQL
Combo29.Requery
, where "CODER" is a field in the dbf file. "CODEK" is also a field in that dbf file, which im comparing with the string kCode.
When I run the code and when I click on the combobox, it asks me to enter arguments instead of showing the selected arguments. The RowSource type is set to Table/Query.
Is the assigning statement incorrect and how can I modify it to show me list of results from the SQL statement?
If I understand your problem correctly kCode is a string in VBA so you'll have to set up your SQL string the following way
sSQL = "SELECT CODER FROM " & dbfname & " IN " & dir & " WHERE TRIM(CODEK) = TRIM('" & kCode & "')"`
as may be quiet aparent, i have very little experience with SQL queries.
I'm having problems with the following Query that i'm generating within my Vb.net application
UPDATE Payments SET B1Code = '12345', ARInvoice = '54321', INV2Go = '00000' WHERE PatientID = '400' AND Product = 'Consultation' AND Catagory = 'Orthotics'
(i have created a test record in the database matching the above information)
Its being constructed with the following code in vb.net:
Dim query As String = "UPDATE Payments SET B1Code = '" & txtB1Code.Text & "', ARInvoice = '" & txtARInvoice.Text & "', INV2Go = '" & txtInv2GoCode.Text & "' WHERE PatientID = '" & Integer.Parse(txtID.Text) & "' AND Product = '" & txtProduct.Text & "' AND Catagory = '" & txtPatientType.Text & "'"
Then being passed the my execute query function like this:
DatabaseFunctions.ExecuteQuery(query)
and the function:
Public Shared Sub ExecuteQuery(ByVal SQL As String)
CheckConnection()
Dim cmd As New OdbcCommand(SQL, con)
cmd.ExecuteNonQuery()
End Sub
The function works perfectly, i've used it time and time again for creating/editing records using simple sql queries built in similar ways as above.. The problem is this particular query returns an error:
ERROR [07002ض] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
Maybe someone with more sql experience than me can see what i'm missing ?
Thanks
This error indicates, that one of the columsn you use in your query does not exist.
Check your query again: did you mean Catagory OR Category ?