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 & "'"
Related
I have a problem on how to insert data into two different table. So my requirements is this.
Under Group Details, The user need to click all the needed information on the dropdown menu and input on the textbox of the table grid view before clicking the ADD Link, after this the page will load displaying the Added Job Title and business group details. The user is allowed to input as many Job title as the user want.
I already finished the table but I have problems in saving the data that I input.
So my first table looks like this Before
and I edit it and this is my table Now
So my problem is this, in my database i have two table. One is EMPGROUP_TBL with columns SEQID, masterID, Business Unit, Division, Sub-Division etc. and the other is EMP_MASTERTBL with columns MasterID, Name, LastName, Jobtitle.
Now everytime I click Add link the jobtitle will not be able to save in the EMP_MASTERTBL so I create a code in VB.Net that will update the EMP_MASTERTBL table when I click the add button under Group Details.
Here's my codes.
If UpdateInsDelRecord("INSERT INTO EMPGROUP_TBL (MASTERID, BUSINESS_UNIT, " & _
"DIVISION, SUB_DIVISION, CLASSIFICATION, SUB_CLASSIFICATION) VALUES " & _
"('" & HandleQuote(Me.lblval_Empid.Text) & "', " & _
"'" & Me.ddl_BusinessUnit.SelectedValue.ToString() & "' ," & _
"'" & val_division & "' ," & _
"'" & val_subdivision & "' ," & _
"'" & Me.ddl_Classification.SelectedValue.ToString() & "' ," & _
"'" & Me.ddl_SubClassification.SelectedValue.ToString() & "')" & _
";" & _
"UPDATE EMP_MASTERTBL SET JOBTITLE = '" & Me.txtJobtitle.Text & "' " & _
"WHERE MASTERID = '" & Me.lblval_Empid.Text & "'") = True Then
Return True
Response.Redirect("eHR_EmpMaintenance.aspx")
Else
Return False
End If
But the user must be able to add as many as Jobtitle and EMPGROUP_TBL details as the user want. So I'm thinking that I'll just write another query for that? How can I add the Group Details and be able to add as many as Jobtitle as the user want?
CheckIfExist
I figured maybe I could use the CheckIfExist and if the employee has an existing data to the jobtitle, business unit, division, sub-division, classification and sub-classification similar to the one that you will add, the messagebox will show that the data already exist. If no data found then it will be able to add the details under the employee's group details. And if you input similar jobtitle but different business unit etc. the data will just be updated and vice versa.
Here's what my code for this.
Function SaveUserGroup() As Boolean
Try
Dim jobtitle As String = Me.txtJobtitle.Text
Dim businessunit As String = Me.ddl_BusinessUnit.SelectedValue
Dim division As String = Me.ddl_Division.SelectedValue
Dim subdivision As String = Me.ddl_SubDivision.SelectedValue
Dim classification As String = Me.ddl_Classification.SelectedValue
Dim subclassification As String = Me.ddl_SubClassification.SelectedValue
Dim CheckMasterTblIfExist As Boolean
Dim CheckGroupTblIfExist As Boolean
Dim insrtResult As Boolean
Dim seqid As String = Me.lblSEQID.Text
Dim emp_id As String = Request.QueryString("emp_id")
If jobtitle <> "" And businessunit <> "Please Select" And division <> "Please Select" And subdivision <> "Please Select" And classification <> "Please Select" And subclassification <> "Please Select" Then
CheckMasterTblIfExist = CheckRecord("SELECT MASTERID, JOBTITLE FROM EMP_MASTERTBL WHERE JOBTITLE = '" & jobtitle & "' AND MASTERID = '" & emp_id & "' ")
CheckGroupTblIfExist = CheckRecord("SELECT * FROM EMPGROUP_TBL WHERE BUSINESS_UNIT = '" & businessunit & "' AND DIVISION = '" & division & "' AND SUB_DIVISION = '" & subdivision & "' AND CLASSIFICATION = '" & classification & "' AND SUB_CLASSIFICATION = '" & subclassification & "' AND MASTERID = '" & emp_id & "' AND SEQID = '" & seqid & "'")
If Not CheckMasterTblIfExist And CheckGroupTblIfExist Then
insrtResult = UpdateInsDelRecord("UPDATE EMP_MASTERTBL SET JOBTITLE = '" & jobtitle & "' " & _
"WHERE MASTERID = '" + Me.lblval_Empid.Text + "'" & _
";" & _
"INSERT INTO EMPGROUP_TBL(MASTERID, BUSINESS_UNIT, " & _
"DIVISION, SUB_DIVISION, CLASSIFICATION, SUB_CLASSIFICATION) VALUES " & _
"('" & HandleQuote(Me.lblval_Empid.Text) & "', " & _
"'" & businessunit & "' ," & _
"'" & division & "' ," & _
"'" & subdivision & "' ," & _
"'" & classification & "' ," & _
"'" & subclassification & "')")
If Not insrtResult Then
MessageBox("alert('Error Ocurred While Inserting a Data.')")
Else
MessageBox("alert('Successfully Added.')")
End If
Else
MessageBox("alert('Data Already Exist.')")
End If
End If
Catch ex As Exception
MessageBox("Error Ocurred while Inserting a data")
Throw
End Try
End Function
I haven't been completed the code yet. I'm in the adding if there's no data and my problem is that the messagebox keeps on telling me that the data already exist even if there's still no employee's group details that added. Please help me with this.
begin tran
if exists (select * from table with (updlock,serializable) where key = #key)
begin
update table set ...
where key = #key
end
else
begin
insert into table (key, ...)
values (#key, ...)
end
commit tran
you can use like this
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])"
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 & '"
using mysql update
Dim OfficerCreated, OfficerID As String
Dim OfficerTodaysDateandTime As Date
OfficerTodaysDateandTime = Now
OfficerID = lvOfficerGrid.SelectedItems.Item(0).SubItems(0).Text
OfficerCreated = lvOfficerGrid.SelectedItems.Item(0).SubItems(5).Text
UpdateOfficer = "UPDATE pcms.users SET user_fname = '" & Me.txtOfficerFname.Text.ToUpper & "', user_mname = '" & Me.txtOfficerMname.Text.ToUpper & "'," & _
"user_lname = '" & Me.txtOfficerLname.Text.ToString & "',user_status = '" & Me.AccountStatus.ToString & "',user_created = '" & OfficerCreated & "',user_updated = '" & OfficerTodaysDateandTime & "' WHERE user_id = " & _
"'" & OfficerID & "'"
OfficerID is an Integer value which is loaded in listview column1
OfficerCreated is a DATETIME which is loaded in listview column5
OfficerTodaysDateandTime is a DATETIME
im getting error in updating..
whats wrong in my codes?
UPDATE table_name SET column1 = value1, column2 = value2
WHERE [where you want to make changes]
to update various column fields use comma-separated, not an ampersand.
And it is also recommended to put a where clause, or every row will be affected with the update.
Here is my code
Dim orderid As Long
orderid = DMax("Number", "Orders", "") + 1
DoCmd.RunSQL "Update Order_temp " _
& "Set Number = " & orderid & ", Name = '" & Me.Textbox & "' " _
& "WHERE (Name = '*')"
It works fine if it's only like this
Dim orderid As Long
orderid = DMax("Number", "Orders", "") + 1
DoCmd.RunSQL "Update Order_temp " _
& "Set Name = '" & Me.Textbox & "' " _
& "WHERE (Name = '*')"
Thank you in advance.
try using square brackets when referencing fields of a table like so :
DoCmd.RunSQL "Update [Order_temp] " _
& "Set [Number] = " & orderid & ", [Name] = '" & Me.Textbox & "' " _
& "WHERE ([Name] = '*')"
It will help avoiding conflicts on reserved words like Number.
Also I would edit a bit the other line as well :
orderid = Nz(DMax("Number", "Orders", ""),0) + 1
In order to avoid a Null exception.