Log in

View Full Version : Help with VBA coding


hwinceFL
Nov 8, 2016, 02:34 PM
This is to request help with VBA coding not working as expected. I am using Access 2007 under MS Win 10 x64. I have a tblAssets (Inventory) with the following structure: AssetID, AutoNumber; PerformanceType, Number; Title, Text; Description, Text; Writer1LastName, Text; Writer1FirstName, Text; Writer2LastName, Text; Writer2FirstName, Text; Writer3LastName, Text; DateofIntro, Date/Time; RightsID, Number; PrimaryLoc, Number; SecondaryLoc, Number; TertiaryLoc, Number; OnHand, Yes/No. Initially each record has a check mark (Yes) in the OnHand field. Then, I use a multi-part form to "issue out" an Asset. The primary/main form has multiple "buttons" to perform necessary functions relating a single record selected by inputs to the form(s). All of the form functions seem to perform satisfactorily (without error); however, one button (Update On Hand Status) does not make the desired change. The Event Procedure coding for that button is:
Private Sub UpdateOH_Click()
Dim strSQL As String
' Set OnHand status to "No" since Asset is being issued out.
strSQL = "UPDATE tblAssets SET tblAssets.OnHand = " & False
End Sub
If further information is needed, I will quickly share it, but I would really appreciate help with where I am going wrong here. Thanks.

ScottGem
Nov 8, 2016, 04:30 PM
You are missing an execute. Assigning a value to strSQL doesn't execute the statement.

Add

CurrentDB.Execute strSQL, dbFailOnError

after the strSQL= line which should be changed to:

strSQL = "UPDATE tblAssets SET tblAssets.OnHand = False;"

hwinceFL
Nov 9, 2016, 09:40 AM
I have made the suggested coding changes and they work; however, not as desired. Although the form using this button is currently pointing to a single record, the Execute instruction effects OnHand status in ALL records of the current table to be set to "No." How can I modify that instruction so that it only updates OnHand status in the current record?

ScottGem
Nov 9, 2016, 09:45 AM
strSQL = "UPDATE tblAssets SET tblAssets.OnHand = False AND [primarykey] = " & Me.primarykeycontrol & ";"

But even better, why not just have a checkbox and uncheck it.

hwinceFL
Nov 22, 2016, 10:51 AM
Hello ScottGem, Please accept my apology. I thought I had already responded to your help. I chose your checkbox suggestion and thus considered the issue RESOLVED. Sorry this didn't get recorded.