I'm trying to UPDATE some fields in my table in Access VBA - sql

I need some help with this, sorry, I am new in VBA and I am trying to run a update query which should obtain a value from a variable and update an already existing table. This is done using VBA. On executing no error message is shown but the table isn't updated with the new value. The code is as follows:
Query = "UPDATE Results " & _
"SET fk_Report='" & Report & "'" & _
",fk_Name='" & Namevar & "'" & _
",fk_Age='" & Agevar & "'" & _
",fk_Sex='" & Sexvar & "'" & _
"WHERE [Count]='" & Countvar & "'" & _
",[Positives]='" & Posvar & "'" & _
",[Negatives]='" & Negvar & "'" & _
",[Unknow]='" & Unkvar & "';"
CurrentDb.Execute (Query)
If somebody can help...

You don't need the commas in the where clause
Query = "UPDATE Results " & _
"SET fk_Report='" & Report & "'" & _
",fk_Name='" & Namevar & "'" & _
",fk_Age='" & Agevar & "'" & _
",fk_Sex='" & Sexvar & "'" & _
"WHERE [Count]='" & Countvar & "' " & _
"AND [Positives]='" & Posvar & "' " & _
"AND [Negatives]='" & Negvar & "' " & _
"AND [Unknow]='" & Unkvar & "';"
CurrentDb.Execute (Query)

use AND instead of , (comma) after WHERE clause

Related

VBA Insert static value into SQL table

I've used VBA to insert into an SQL table before but its always been using values in cells. I have three columns in my table where I need to add just a static value (so no cell reference).
The code I have so far is:
stSQL = "INSERT INTO Client_Notes " & _
"(ID, Category, RefID, Name, Notes, Date_Loaded, Latest) " & _
" VALUES (" & _
"'" & 1 & "', " & _
"'" & 1 & "', " & _
"'" & Sheets("Missing").ActiveCell.Offset(0, -10).Value & "', " & _
"'" & Sheets("Missing").ActiveCell.Offset(0, -9).Value & "', " & _
"'" & Sheets("Details").ActiveCell.Value & "', " & _
"'" & Date & "', " & "'" & _
"'" & 1 & "'" & _
")"
The error I keep getting is when trying to insert 1 as a value. I'm sure its straight forward but feel like I had tried every quote and comma combination going.
Thanks in advance
So the new code now looks like so, where I am getting Subscript Out Of Range:
stSQL = "INSERT INTO Client_Notes " & _
"(ID, Category, RefID, Name, Notes, Date_Loaded, Latest) " & _
" VALUES (" & _
"1, " & _
"1, " & _
"'" & Sheets("Missing").Range("L" & i).Offset(0, -10).Value & "', " & _
"'" & Sheets("Missing").Range("L" & i).Offset(0, -9).Value & "', " & _
"'" & Sheets("Details").Range("L" & i).Value & "', " & _
"'" & Date & "', " & _
"1)"

ms access 2007 update error run time error 3464, data type mismatch in criteria expression

i cannot update the data, it says that data type mismatch in criteria experession
CurrentDb.Execute "UPDATE PC_Details " & _
" SET Serial_No=" & Me.txtNo & _
", PC_Brand='" & Me.txtBrand & "'" & _
", PC_Model='" & Me.txtModel & "'" & _
", Status='" & Me.txtStatus & "'" & _
", Description='" & Me.txtDesc & "'" & _
", Staff_ID='" & Me.txtID & "'" & _
" WHERE Serial_No=" & Me.txtNo.Tag
Do this to debug:
Dim SQL As String
SQL = "UPDATE PC_Details " & _
" SET Serial_No=" & Me.txtNo & _
", PC_Brand='" & Me.txtBrand & "'" & _
", PC_Model='" & Me.txtModel & "'" & _
", Status='" & Me.txtStatus & "'" & _
", Description='" & Me.txtDesc & "'" & _
", Staff_ID='" & Me.txtID & "'" & _
" WHERE Serial_No=" & Me.txtNo.Tag
Debug.Print SQL
CurrentDb.Execute SQL
Then study the resulting SQL and post it here.
If all values are present, one or more is text but is handled as number or vice versa.

Syntax error in UPDATE statement vb and ms.access as database

I am trying to make medical record application for my assignment. I found this error to edit data patient.
Please help and sorry for my bad grammar
Dim aksesedit As String = "Update tbpasien set " & _
"Nama_Pasien='" & txtnamapasien.Text & "', " & _
"Jenis_Kelamin='" & cmbjk.Text & "'," & _
"Tempat_Pasien='" & txttempatlahir.Text & "', " & _
"Tanggal_Lahir='" & tanggallahir.Text & "', " & _
"Alamat='" & txtalamat.Text & "', " & _
"Kelurahan_Desa='" & txtkeldesa.Text & "', " & _
"Kecamatan='" & txtkec.Text & "', " & _
"Kota_Kabupaten='" & txtkotakab.Text & "', " & _
"No_Telepon_HP='" & txtnotelp.Text & "', " & _
"Agama='" & cmbagama.Text & "', " & _
"Kewarganegaraan='" & cmbwarga.Text & "', " & _
"Status_Pernikahan='" & cmbstatus.Text & "', " & _
"Pekerjaan='" & txtpekerjaan.Text & "', " & _
"where No_RM='" & txtnorm.Text & "'"
cmd = New OleDbCommand(aksesedit)
cmd.Connection = conn
cmd.ExecuteNonQuery()
syntax error in update was on here
cmd.ExecuteNonQuery()
For "Tanggal_Lahir" i used datetimepicker, cmb=combobox, txt=textbox
Thanks
There should be no comma before the WHERE clause:
"Pekerjaan='" & txtpekerjaan.Text & "' " & _
"where No_RM='" & txtnorm.Text & "'"

VBA SQL Join Query

I am having troubles with a VBA SQL JOIN. I Keep Getting A "Join Expression Not Supported" Error. The Following Code Works In The Query Design View but seems to throw an error when in vba.
Dim Rs As DAO.RecordSet
Set Rs = CurrentDb.OpenRecordset( _
"SELECT Schools.ID, Schools.[School Name],Schools.Address, Schools.Postcode, Schools.[Principal name], " & _
"Schools.[E-Mail], Schools.Phone, Schools.Region, Schools.JTHE, Schools.[Social Status], Events.Program " & _
"FROM Schools INNER JOIN Events ON Schools.ID = Events.School WHERE ((Schools.Region = '" & RegionOne & _
"' Or Schools.Region = '" & RegionTwo "' Or Schools.Region = '" & RegionThree "' Or Schools.Region = '" & _
RegionFour "') AND (Schools.JTHE = " & JTHE1 & " Or Schools.JTHE = " & JTHE2 ") AND (Schools.[Social Status] = '" & _
StatusBox.Value "') AND (Events.Program = '" & ProgramBox.Value & "'));")
This Similar Query Works
Set Rs = CurrentDb.OpenRecordset("SELECT * FROM Schools WHERE " & _
"(((Schools.Region)='" & RegionOne & _
"' Or (Schools.Region)='" & RegionTwo & _
"' Or (Schools.Region)='" & RegionThree & _
"' Or (Schools.Region)='" & RegionFour & _
"') AND ((Schools.[Social Status])='" & StatusBox.Value & _
"') AND ((Schools.JTHE)=" & JTHE1 & " Or (Schools.JTHE)=" & JTHE2 & "));")
Any help would be greatly appreciated.
I'm not entirely sure why is that. It is hard to spot error when your doing it on VBA, unlike if your in an actual SQL Management studio where you can spot the lines that errors out. Nonetheless, you may try this:
Set Rs = CurrentDb.OpenRecordset( _
"SELECT Schools.ID, Schools.[School Name], Schools.Address, " & _
"Schools.Postcode, Schools.[Principal name], Schools.[E-Mail], " & _
"Schools.Phone, Schools.Region, Schools.JTHE, Schools.[Social Status], " & _
"Events.Program " & _
"FROM Schools " & _
"INNER JOIN Events " & _
"ON Schools.ID = Events.School " & _
"WHERE Schools.Region IN (" & _
"'" & RegionOne & "'," & _
"'" & RegionTwo & "'," & _
"'" & RegionThree & "'," & _
"'" & RegionFour & "') " & _
"AND Schools.JTHE IN (" & JTHE1 & ", " & JTHE2 & ") " & _
"AND Schools.[Social Status]='" & StatusBox.Value & "' " & _
"AND Events.Program='" & ProgramBox.Value & "';")
I formatted it as such to give you the story of the query (and that is how I will write it in SQL). Not really a direct to the point answer to your question but I just simplified your OR statements and instead uses IN. You might get a:
Too many continuous line error
So adjust the concatenation of strings. I have not tested this of course (although it compiles) but my goal is to give you idea on a possible way to do it. HTH.

Using variables in fieldnames - SQL Update statement

I am new to using Access 2010. I wish to execute the following sql update statement, but I have problems with the syntax. The table is called "Forecasts", and users will edit & update the qty forecasted.
Problem - The table fieldnames are 2014_1, 2014_2, 2014_3 ... to represent the different months, stored in an array. I have done abit of research and I believe the way to dynamically do this is:
Dim sqlString As String
sqlString = "UPDATE Forecasts " & _
" SET Branch_Plant=" & Me.txtBranch_Plant & _
", Item_Number_Short='" & Me.txtItem_Number_Short & "'" & _
", Description='" & Me.txtDescription & "'" & _
", UOM='" & Me.txtUOM & "'" & _
", Estimated_Cost=" & Me.txtEstimated_Cost & _
", Requesting_Business_Unit='" & Me.txtRequesting_Business_Unit & "'" & _
", End_Customer='" & Me.txtEnd_Customer & "'" & _
", Project='" & Me.txtProject & "'" & _
", Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume " & _
" WHERE ID =" & Me.txtID.Tag
MsgBox ("This is the output: " & sqlString)
CurrentDb.Execute sqlString
It was working fine until this line was added
Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume
The msgbx output now shows: "False". Whats wrong with sqlString?
Please help! Thank you very much.
", Forecasts.[" & arrMonthToDisplay(0) & "] = " & Me.txtProjectedJanVolume & _
" WHERE ID =" & Me.txtID.Tag
You compare string to string.
Change
", Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume " &
to
", Forecasts." & "[" & arrMonthToDisplay(0) & "] = " & " Me.txtProjectedJanVolume " &