How do I update MySQL from visual studio 2010? - mysql

I need to update a MySQL database with the new values just collected.
The program will get values like this:
sql = "select * from Accounts where username ='" & txtuname.Text & "' and userpassword = '" & txtpass.Text & "'"
Dim credits As String
credits = publictable.Rows(0).Item(5)
credit.Text = "" & credits
Then it will update the current value with a new value like this:
cmd.CommandText = "UPDATE * Accounts SET credits = '& credit.Text + 10 &' WHERE username = '& Form1.txtuname.Text & '"
However, I am stuck on the UPDATE string.

I think, you have to study well about the update command, since your query syntax is wrong. and you are making some syntactical mistake in the formation of the query. use Line continuation operators to make the query more understandable. plan the output before you query.
cmd.CommandText = "UPDATE Accounts SET " & _
"credits = '" & val(credit.Text) + 10 & "'" & _
"WHERE username = '" & Form1.txtuname.Text & '"

Related

MySQL: Search a value in a first table. If not exist, search it in second table

I want to identify account inputted if it is an admin account or an user account and search the username and password entered in admin table if it is not there search it into "users table"and this is the error i got operand should contain 1 column(s)
this is the query i tried
Query = "select admin.user,admin.pass from admin where user= '" & username.Text & "' and pass= '" & password.Text & "'" not in users.user,users.pass"
The error is pretty obvious: only single column allowed to pass for IN or NOT IN clause to determine relevant values assigned for that column.
You can fix the query by providing single column from a subquery like this example:
Dim Query As String = "SELECT admin.user, admin.pass FROM admin WHERE user = '" & username.Text & _
"' AND pass = '" & password.Text & _
"' OR user [NOT] IN (SELECT users.user FROM users WHERE user = '" & username.Text & _
"' AND pass = '" & password.Text & "')"
NB: Rather than concatenating value strings, use parameterized query version instead:
Dim Query As String = "SELECT admin.user, admin.pass FROM admin WHERE user = #username AND pass = #password OR user [NOT] IN (SELECT users.user FROM users WHERE user = #username AND pass = #password)"
Similar issue:
Operand Should Contain 1 Column - MySQL NOT IN

Can't find what is wrong with the code

I Have this code, and I can't figure out what is wrong with it. It does not return any error but field Date_Returned is not getting updated. Please help.
Private Sub txtbxret_Click()
Dim query As String
query = "UPDATE Rent SET Date_Returned = '" & Date & "' WHERE Date_Rent = " & txtrented.Value & " AND Customer_ID = " & txtbxcustID.Value & " AND Movie_ID = " & txtbxmovID.Value
DoCmd.RunSQL (query)
End Sub
I've double and triple checked all the field names and thay are ok by the way...
You must use proper formatting of string expressions of date values in SQL:
query = "UPDATE Rent SET Date_Returned = Date() WHERE Date_Rent = #" & Format(txtrented.Value, "yyyy\/mm\/dd") & "# AND Customer_ID = " & txtbxcustID.Value & " AND Movie_ID = " & txtbxmovID.Value

MS Access VB: How do I update a table with a parametrized query ONLY with textboxes that are not empty?

I have some VBA code that will update a table based on a form. It works great, except that I want the table to only update wherever the user fill in info. If that textbox is instead blank, do no update that field in the table. Below is the working update code. The line that starts with If query.Parameters("P1")... is my attempt at trying to make an If statement that will find which textbox values are Null and then ignore those, but I don't know if that's even going in the right direction.
Private Sub Command133_Click()
'Update row for downtime
Dim dbsCurrent As Database
Set dbsCurrent = CurrentDb
', suffix, production_date, reason, downtime_minutes, comment ,'" & CInt(Me.Text118) & "','" & CDate(Me.Text126) & "','" & Me.Text121 & "','" & CDbl(Me.Text123) & "','" & Me.Text128 & "'
'dbsCurrent.Execute " INSERT INTO tbl_Downtime (production_date) SELECT #" & Me.Text126 & "# FROM tbl_Downtime As t WHERE t.ID = " & Me.Text135 & ";"
'dbsCurrent.Execute "UPDATE tbl_Downtime SET job = '" & Me.Text116 & "', suffix = '" & Me.Text118 & "', production_date = #" & Me.Text126 & "#, reason = '" & Me.Text121 & "', downtime_minutes = " & Me.Text123 & ", comment = '" & Me.Text128 & "', shift = '" & Me.Text144 & "' WHERE t.ID = " & Me.Text135 & ";"
Dim query As QueryDef
Dim sql As String
For Each query In CurrentDb.QueryDefs
If query.Name = "UpdateDowntime" Then
Exit For
End If
Next query
If query Is Nothing Then
sql = "parameters " & _
"P1 text, P2 text, P3 Date, P4 Text, P5 Number, P6 Text, P7 Text, P8 Number;" & _
"UPDATE [tbl_Downtime] " & _
"SET job = [P1], suffix = [P2], production_date = [P3], reason = [P4], downtime_minutes = [P5], comment = [P6], shift = [P7] " & _
"WHERE[tbl_Downtime].id = [P8]"
'"(job, suffix, production_date, reason, downtime_minutes, comment, shift) " & _
'" VALUES ([P1], [P2], [P3], [P4], [P5], [P6], [P7]) WHERE[tbl_Downtime].id = [P8]"
Set query = CurrentDb.CreateQueryDef("UpdateDowntime", sql)
End If
query.Parameters("P1").Value = Me.Text116
query.Parameters("P2").Value = Me.Text118
query.Parameters("P3").Value = Me.Text126
query.Parameters("P4").Value = Me.Text121
query.Parameters("P5").Value = Me.Text123
query.Parameters("P6").Value = Me.Text128
query.Parameters("P7").Value = Me.Text144
query.Parameters("P8").Value = Me.Text135
If query.Parameters("P1").Value = "" Then Set query.Parameters("P1").Value = job End If ' WHERE [tbl_Downtime].id = [P8] END
query.Execute
An empty string is not the same as a null value in a database, so you need to change the parameter value to nothing if you want it to equate to null (or default value if one exists).
You could either conditionally set the parameter value like this:
If textbox1.text <> "" then
query.Parameters("P1").Value = textbox1.text
End if
or you could write a function to set the parameter to nothing if the textbox is empty:
Function NothingIfEmpty(value As String)
If value = "" Then
NothingIfEmpty = Nothing
Else
NothingIfEmpty = value
End If
End Function
and use it like this:
query.Parameters("P1").Value = NothingIfEmpty(textbox1.text)
query.Parameters("P2").Value = NothingIfEmpty(textbox2.text)
You can do it this way:
"SET job = Nz([P1], job), suffix = Nz([P2], suffix), production_date = Nz([P3], production_date), reason = Nz([P4], reason), downtime_minutes = Nz([P5], downtime_minutes), comment = Nz([P6], comment), shift = Nz([P7], shift) " & _
"WHERE [tbl_Downtime].id = Nz([P8], -[id])"

WHERE clause with a Max(Date) functionality in VBA, SQL Back-end, Access front-end

Within Access, I am trying to build a WHERE clause with a Max(Date) functionality, but it is not working. Any help would be greatly appreciated.
strSQL1 = "Update tTbl_LoginSessions SET fldLogoutEvent = '" & Now() & "'" & _
" WHERE fldUserName = " & IntMSIDAutoNum And fldLoginEvent = MAX(fldLoginEvent)
fldLoginEvent is the date/time in which the User Logged in
fldLogoutEvenr is the date/time in which the User will be logged out
The most obvious correction:
strSQL1 = "Update tTbl_LoginSessions SET fldLogoutEvent = '" & _
Now() & "'" & " WHERE fldUserName = " & IntMSIDAutoNum & _
" And fldLoginEvent = (select top 1 fldLoginEvent from " & _
" tTbl_LoginSessions )"

MYSQL Update Command via VBScript

Having problems getting a csv imported into a MYSQL database to update existing data. The importing new data works fine, just getting errors updating.
`CSVSerial = arrServiceList(0)
CSVAssetTag = arrServiceList(1)
CSVLocation = arrServiceList(2)
CSVRoom = arrServiceList(3)
CSVCheckedOutTo = arrServiceList(4)
commandText = "Update Inventory Set Room = CSVRoom, Location = CSVLocation, AssetTag = CSVAssetTag, CheckedOutTo = CSVCheckedOutTo where SerialNumber = CSVSerial"`
I've tried doing $CSVSerial, '$CSVSerial', 'CSVSerial', they don't seem to recognize any of them. If I do a
Msgbox (CSVSerial & "," & CSVRoom & "," & CSVLocation & "," & CSVAssettag & "," & CSVCheckedOutTo)
all the data is correct there. I'm sure it's something simple, just don't have alot of MySQL experience.
Thanks
Your query just uses the literal strings CSVRoom, CSVLocation, etc. rather than their values. The $CSVSerial won't work in VB - that's a PHP thing (plus some other languages). In addition, if these are string values they need to be surrounded by single quotes. Try something like this instead:
commandText = "Update Inventory Set Room = '" & CSVRoom & "', " & _
"Location = '" & CSVLocation & "', " & _
"AssetTag = '" & CSVAssetTag & "', " & _
"CheckedOutTo = '" & CSVCheckedOutTo & "' where SerialNumber = '" & CSVSerial & "'"
I've assumed all the columns are strings. If any are numbers you don't need the single quotes around them but then again they shouldn't hurt either.
Addendum: Followup question is how to update the columns only if they're not blank or null.
To test for blank or null, you can use a condition like this:
Room IS NULL OR Room = ''
... or like this:
COALESCE(Room, '') = ''
The second one is shorter so I'll go with that because the command lines are getting pretty long. The basic MySQL command goes like this:
UPDATE Inventory SET
Room = CASE WHEN COALESCE(Room, '') = '' THEN Room ELSE 'new value' END,
...
So if Room is '' or NULL it's set to its existing '' or NULL value. If not, it's set to the new value.
Here's how it looks in VBScript. This is untested because I don't have VBScript available.
commandText = "UPDATE Inventory SET " & _
"Room = CASE WHEN COALESCE(Room, '') = '' THEN Room ELSE '" & CSVRoom & "' END, " & _
"Location = CASE WHEN COALESCE(Location, '') = '' THEN Location ELSE '" & CSVLocation & "' END, " & _
"AssetTag = CASE WHEN COALESCE(AssetTag, '') = '' THEN AssetTag ELSE '" & CSVAssetTag & "' END, " & _
"CheckedOutTo = CASE WHEN COALESCE(CheckedOutTo, '') = '' THEN CheckedOutTo ELSE '" & CSVCheckedOutTo & "' END " & _
"WHERE SerialNumber = '" & CSVSerial & "'"