When you do a RunSQL Update "tablename" Set Field1='" & Me.text1 & "', Field2='" & Me.text2 & "' etc etc , do the field names need to be in the same order as they go in the table, and what happens if you are only updating say the 2nd field 4th field and the last two fields ?
This relates to a problem I am having with using the Update function, but I just want to make sure I'm not doing this all wrong from the start !
Thanks for that, yep i'm already using the debug.print but it's not giving me any clues as to why things aren't working.
Basically the last two fields that I am trying to update, which are empty before updating, will not save, the second from last is a date field, and I am just now swapping it to the last entry to see if this is what's stopping it from working.
I will post a much bigger explanation once i've extinguished all my brain cells :banghead:
StockData table contains multiple columns of which I want only a few to go into the repair table.
RepairData table has 4 fields taken from StockData (PC number,serial number,type etc) and stores extra fields for repair information.
Workshop form has a subform "WorkshopSubForm" with the source from "RepairDataQuery"
Workshop form has a button "Add a device for repair" which launches "AddPC2RepairSearchForm" which allows the user to enter a PC number or serial number, check to see if either are valid, and if so opens a form "AddPC2RepairDetailForm"
"AddPC2RepairDetailForm" displays data that is not changeable i.e. PC number,serial number,type etc. with a few unbound text input boxes "Fault Description", "BookedinDate" etc.
I then have a button called "Add Device to the Repair List" with this code:
This adds all the data from the form including the data entry from the unbound text boxes and creates a new record in the RepairData table which has an
autonumber "ID" primary field.
So far so good, this all works perfectly, minus a few tweaks I want to add later.
Going back to "WorkshopForm" this will now diplay the data which was added by the above.
There is another button "Update Repair Details" which opens "UpdateRepairForm" based on the records ID.
"UpdateRepairForm" gets it's data from "RepairDataQuery" which in turn queries "RepairData" table with some filters, again so far so good.
"UpdateRepairForm" allows for some of the original fields to be changed (others are locked like the job ref and bookedindate) it has a "RepairStatus" combo box and two new unbound text input boxes :"RepairDate" and "RepairDescription" RepairDate allows for manual input or there is a button "Today" that puts today's date in, and RepairDescription in manual entry.
I then have another button "Update Repair" that amends the current record - this is where things go wrong.
When I click the button, it immediately comes up with this message box : Write Conflict "This record has been changed by another user since you started editing it. If you save the record, you will overwrite the changes the other user made. ..."
There are 3 buttons "Save Record" "copy to clipboard" "Drop Changes" , naturally I click "save record"
Now if I have only changed one of the fields that already existed, and after I get the above message, the changes have been saved to the table, but if I enter in the date and repair description into the form, they never save to the RepairData table ?
Code as follows:
Code:
Private Sub cmdupdaterepair_Click()
If (Me.RepairStatus) = "In Repair Queue" Then Me.Status.Value = "Waiting Repair"
If (Me.RepairStatus) = "Investigating Fault" Then Me.Status.Value = "Under Repair"
If (Me.RepairStatus) = "Being Tested" Then Me.Status.Value = "Under Repair"
If (Me.RepairStatus) = "Being Repaired" Then Me.Status.Value = "Under Repair"
If (Me.RepairStatus) = "Being Risd" Then Me.Status.Value = "Under Repair"
If (Me.RepairStatus) = "Repaired" Then Me.Status.Value = "Stock"
Rem Me.Location.Value = "L07"
Dim strSQL As String
DoCmd.SetWarnings False
strSQL = "UPDATE RepairData " & _
"SET OSType='" & Me.ostype & "', Location='" & Me.Location & "', User='" & Me.User & "', DepartmentName='" & Me.DepartmentName & _
"', Status='" & Me.Status & "', RepairStatus='" & Me.RepairStatus & "', RepairDescription='" & Me.RepairDescription & "', RepairDate='" & Me.RepairDate & "' " & _
"WHERE ID=" & Me.ID & ";"
Debug.Print strSQL
DoCmd.RunSQL strSQL
DoCmd.Close acForm, "UpdateRepairForm"
DoCmd.SetWarnings True
DoCmd.OpenForm "WorkShopForm"
End Sub
Sorry, I started to type this all out in notepad and the formatting has gone a bit iffy copying it into here !
Please let me know if other info would be helpful.
I have no clue as to why I decided to use the SQL UPDATE statement, I guess because I had spent a day figuring out the SQL INSERT INTO statement I thought that's the way to do the next bit ! - wrong
When you said "Typically you would do one or the other" the penny dropped !
This is what happens when you don't look at Access for 6 months - stupidity sets in
Got it working correctly (doing it the correct way) in 2 minutes !!
AH well I learnt a few things on the way, SQL Insert into and Update statements are a PITA getting the syntax right, and as Einstein said human stupidity is infinite