How to detect what control triggers BeforeUpdate event in Access 2007 - vba

I have an audit trail that uses the BeforeUpdate event to track changes made on a subform using the following code:
`Dim USR As String
Dim TS As Date
Dim Connection As ADODB.Connection
Dim RecordSet As ADODB.RecordSet
Dim Ctl As Control
MsgBox "Here!"
Set Connection = CurrentProject.Connection
Set RecordSet = New ADODB.RecordSet
If Forms![PartsDatabaseX]![RepsSubformX].Visible = True Then
For Each Ctl In Screen.ActiveForm.RepsSubformX.Form.Controls
If Ctl.Tag = "Track" Then
If Nz(Ctl.Value) <> Nz(Ctl.OldValue) Then
SaveToken = True
With RecordSet
.AddNew
![Part Number] = Screen.ActiveForm.RepsSubformX.Form.Controls("[Part Nbr]").Value
![Record Identifier] = Screen.ActiveForm.RepsSubformX.Form.Controls("[Part Nbr]").Value & Screen.ActiveForm.RepsSubformX.Form.Controls("[Supplier Name]").Value
![Rep] = USR
![Time Stamp] = TS
![Change Point] = Ctl.ControlSource
![Change From] = Ctl.OldValue
![Change To] = Ctl.Value
.Update
End With
End If
End If
Next Ctl
End If`
The problem I am having is that is the user makes two changes there are three things recorded in my change history table - the first change to the record twice and the second change to the record once (this trend continues as long as the user never leaves the record). What I would like to do is be able to identify the control that triggered the BeforeUpdate event and pass it to the code above so it can check if only the control that triggered the BeforeUpdate event is different and skip the others that have already been logged. Alternatively, is there a way to prevent Access from seeing logged changes as new?

Forms and controls have Order of Events :
Similarly, when you close a form, the following sequence of events
occurs:
Exit (control) → LostFocus (control) → Unload (form) → Deactivate
(form) → Close (form)
If you've changed data in a control, the BeforeUpdate and AfterUpdate
events for both the control and the form occur before the Exit event
for the control.
You may wish to read http://support.microsoft.com/kb/197592

The answer has been staring me in the face the whole time... When I assign the BeforeUpdate event to each control, I can make it pass a variable to the function it calls to tell me the program what sent it:
Forms![PartsDatabaseX]![RepsSubformX].Form![Pack Rank].BeforeUpdate = "=ToTracking(""Pack Rank"")"
After that, it's a simple matter of adding an and statement when checking for changed values so it only captures the change that set off the BeforeUpdate event like so:
If Nz(Ctl.Value) <> Nz(Ctl.OldValue) And Ctl.ControlSource = NameOfTrigger Then
'Record Values
End if

Related

How do I change the _Click() event on a MS Access Form?

I am hoping to be able to streamline my UI. I want to have a set of command buttons change the _On_Click() event based on a user selection. For example:
Main topic selections: cmd1:"Membership Reports", cmd2: "Administration Reports", cmd3: "Other Reports - TBD"
If the user selects cmd1 then the subtopic buttons properties change to allow the user to open reports in that category.
Sub Topic Selections: cmd4: "All Members", cmd5: "Active Members", etc.
If the user selects cmd2: then the on_Click event would change to open reports in the "Administration Reports" group.
Thanks in advance for your help.
I would use 3 main Toggle Buttons and put hem into an Option group frame (Let's call it FrameMain). Set the Option Value for the buttons as 1,2,3. Create as many regular buttons a you have sub topics (let's call them cmd1_1, cmd1_2, cmd2_1....) and set theirs property Visible to False and Tag to Sub. Now create event FrameMain_AfterUpdate:
Private Sub FrameMain_AfterUpdate()
Dim ctl As Control
For Each ctl In Me.Controls
If ctl.Tag = "Sub" Then
ctl.Visible = False
End If
Next
Select Case Me.FrameMain
Case 1
cmd1_1.Visible = True
cmd1_2.Visible = True
Case 2
cmd2_1.Visible = True
cmd2_2.Visible = True
Case 3
cmd3_1.Visible = True
cmd3_2.Visible = True
End Select
End Sub
You can create now On_Click() event for all your sub-buttons to open the report you want.
You can also use a Switch board (search the Internet how to create it).

Sub to add controls works when called from one sub but the other

I am getting an error that I just can't figure out. I wrote a sub that opens a form in Design view, deletes all of the dynamic controls, then adds the requested number.
The sub gets called in two different ways. The user opening the form (via a Main Menu form) to fill out a new form (so the dynamic controls are deleted then recreated) OR after the form is created, the user can click a button on the form to add more rows of controls. Both the Main Menu and button form call the same sub, BUT when the button is clicked the code gets stuck and error 29054 'Microsoft Access can't add, rename, or delete the control(s) you requested.' is thrown. The button to debug is deactivated so I can't see what line is actually getting stuck, but when I step through the code this is the last line before the error pops up (the last indent block in the context below):
With Application.CreateControl("BOM5", acComboBox, acDetail, frm.Controls("Tabs").Pages(PageNum).Name, , ChangeTypeLeft, LastControlTop, ChangeTypeWidth, ControlHeight)
The rest of the code is as follows.
DoCmd.OpenForm "BOM5", acDesign
Set frm = Application.Forms("BOM5")
' Cycle through controls and set LastControlTop based on the last dynamic control, if any
For i = 0 To frm.Controls.Count - 1
Set ctl = frm.Controls(i)
Debug.Print ctl.Name
If ctl.Tag Like DYNAMIC_TAG & "*" Then
If ctl.ControlType = acComboBox Or ctl.ControlType = acTextBox Then
LastControlTop = ctl.Top + ControlHeight + ControlPadding
FormRowCount = FormRowCount + 1
End If
Else
FormRowCount = (FormRowCount / 6) + 1 ' Convert number of fields to number of rows then add one to start new batch of controls
Exit For
End If
Next
PageNum = frm.Controls("Tabs").Pages.Count - 1 ' .Pages has an index of 0. Getting PageNum to follow suit with the -1
' Add controls for inputting parts
For FormRowCount = FormRowCount To NewControlCount
With Application.CreateControl("BOM5", acComboBox, acDetail, frm.Controls("Tabs").Pages(PageNum).Name, , ChangeTypeLeft, LastControlTop, ChangeTypeWidth, ControlHeight)
.Name = "ChangeType" & FormRowCount
.Tag = DYNAMIC_TAG
.RowSourceType = "Table/Query"
.RowSource = "ChangeType"
End With
The last for loop has 5 other With Application.CreatControl statements, but I just showed the first one. The other 5 are similar except text boxes instead of combo.
I've had this error before, but I think I resolved it by moving the DoCmd.OpenForm statement to a different part of the code (like out of an if statement or for loop or somewhere that wasn't letting it get called) but I don't think that will resolve it. Besides, the first for loop iterates correctly since I see it grabbing the control height of the last dynamic control.
You can't do this. A form or report can only hold a certain amount of controls, deleted or not, so - eventually - it will not accept more controls. At that point, you must recreate the form from scratch.
So, don't delete the controls. Create those you may need and, at any time, hide those not needed and rename the rest if necessary.

Having trouble with show/hide/visible in VB .net It only works the first time

I have a multi form application I'm close to finishing, but I have a HUGE problem.
I need to keep a main form open, basically a communications task, that opens other forms that display general status and asks for various user input. The problem is that when any form is requested either automatically by the status from the communications engine, or via user button press. The first time a form is requested there is no problem, however the second time any given form is requested, it shows on the windows task bar, but will not open on the screen. All form switching in this application is handled by the same routine.
NOTE: The form named "WEDGE_MENU_SCREEN" is the form that stays open and active, only one other form should be active at any time.
Here is the code.
Public Function GoToForm(Requested_form As Form) As Boolean
'in a multi form app, we only want ONE form active on screen at any given time.
'all screen change requests should come from the same operating thread
Dim Not_found_form As Boolean = True
' Dim frm As Form
Dim formNames As New List(Of String)
Dim xformNames As New List(Of String)
'get a list of active forms running under this main thread LESS the static one
For Each currentForm As Form In Application.OpenForms
If currentForm.Name <> "WEDGE_MENU_SCREEN" Then
formNames.Add(currentForm.Name)
End If
Next
'from that list create another less the requested in the case that the requested form is already up
For Each currentFormName As String In formNames
If currentFormName <> Requested_form.Name Then
xformNames.Add(currentFormName)
End If
Next
'if the second list is not empty
If xformNames.Count > 0 Then
'hide all active forms in the second list
For Each currentFormName As String In xformNames
Application.OpenForms(currentFormName).Visible = False
Application.OpenForms(currentFormName).Opacity() = 0
Next
End If
'then activate the requested form
Requested_form.Visible = True
Requested_form.Opacity = 1
Return True
End Function
FURTHER NOTE: I have tried the following as well
Application.OpenForms(currentFormName).Hide()
with Requested_form.Show()
ALSO
Application.OpenForms(currentFormName).Close()
with Requested_form.Show()
Any help would be greatly appreciated.
-Jim
Maybe if you make the logic a bit simpler it would help. The 3 loop can be combined into one.
For Each currentForm As Form In Application.OpenForms
If currentForm.Name <> "WEDGE_MENU_SCREEN" AndAlso currentForm.Name <> Requested_form.Name Then
currentForm.Visible = False
currentForm.Opacity() = 0
End If
Next

Can not update List Box Selected Collection in code

I have a list box on a form being populated from a query, with items selected on basis of matching a delimited list. So sSystemString equals something like "A;B;C"
Then I load records A,B,C,D,E,F from the SQL Server DB and only A,B,C should be selected.
Is there a native way to do this in MS Access (2010). I'm using an ADP in this case.
I'm doing it via code but I can't the selected property does not reflect my changes, nor does the form.
Here's my code:
Dim rs As New ADODB.Recordset
Dim sSystemString As String
If Not IsNull(Me.OpenArgs) Then sSystemString = Me.OpenArgs
' Load this list box with SRC Systems
Call rs.Open("SELECT DISTINCT System FROM dbo.System WHERE System IS NOT NULL", _
CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly)
Do Until rs.EOF
lstSrcSystems.AddItem (rs.Fields(0))
If InStr(sSystemString, rs.Fields(0)) > 0 Then
lstSrcSystems.Selected(lstSrcSystems.ListCount - 1) = True
End If
rs.MoveNext
Loop
My code definitely hits the lstSrcSystems.Selected(lstSrcSystems.ListCount - 1) = True line.
After running this line, inspecting the property in the immediate window still returns 0 (it doesn't change). On the form, the item is also not selected.
UPDATE: I just checked my code again and now it is being updated, but the next AddItem apparently unselects it again.
I suspect I have some weird combination of properties that make this read only, but I can select items interactively, and indeed when I extract the selected items back off in code, the Selected property works as expected - i.e. I select an item on the form and it reflected in this property.
The form is unbound and is called from a button on another form with this code:
DoCmd.OpenForm "fSiteList", acNormal, , , acFormEdit, acDialog, Me.SRCSystems
The problem was actually that lstSrcSystems.AddItem (rs.Fields(0)) was resetting the Selected state. I can't find any mention of this behaviour or how to turn it off. I altered my form as follows:
Form / Data properties all blank (unbound)
List Control / Data / Control Source: blank
List Control / Data / Row Source: SELECT statement populating my list
This has the effect of populating the list but not binding the list or form to anything. (I found that binding it stopped me interactively editing data on it)
Form_Load code behind the form was changed to select already existing items:
Private Sub Form_Load()
Dim sSystemString As String
Dim iIndex As Integer
' List is bound to site list (from connections)
' Highlight those that are listed
If Not IsNull(Me.OpenArgs) Then sSystemString = Me.OpenArgs
iIndex = lstSrcSystems.ListCount
Do While iIndex > 0
If InStr(sSystemString, lstSrcSystems.ItemData(iIndex)) > 0 Then
lstSrcSystems.Selected(iIndex) = True
End If
iIndex = iIndex - 1
Loop
End Sub
I'm still curious to know whether there is a more 'built in' way to achieve this: edit a delimited string field.

Close modal form in Access and access module code

I've inherited a Access database that is used to import some data in SQL. The MDB opens with a form, in a modal mode: no Access menubar or buttons are visible. I can view the tables with content by using the Visual Studio 'Data Connection' tool, but I cannot see the module's code.
I've looked at this question here, but the answers there aren't really what I need. Is there a way to either force the form to close (and access the modules) or to extract the VBA code ?
[EDIT] I am using Access 2007, not sure what the original developer used.
Hold down the shift key when you open the database. This will prevent it from loading the automatic script it's running and allow you to gain access to the tables, queries, and VBA scripts.
This is a rather long addendum to, or comment on, Michael Todd's reply in response to edosoft's comment.
Rather than choosing to enable the content, check the start-up options (either (file->options->current database->display form) or (tools->start up->display form)) and remove the name of the form, having taken a note, and ensure that Allow Full Menus (same page) is ticked. You may also like to press Alt+F11 to display code, and check that for start-up code, finally, see if there is an AutoRun macro and rename it.
EDIT re Comments
You do not have to open an mdb to change the start-up form, for example, code such as this could be run from another mdb using the full name and path of the mdb you wish to change.
Sub SetStartForm(DBFile As String)
Dim prp As Object
Dim db As Database
Const PROPERTY_NOT_FOUND As Integer = 3270
Set db = OpenDatabase(DBFile)
db.Properties("StartupForm") = "(none)"
If Err.Number > 0 Then
If Err.Number = PROPERTY_NOT_FOUND Then
'' Create the new property, but this is not relevant in this case
End If
End If
db.Close
Set db = Nothing
Set prp = Nothing
End Sub
Button close with function:
Private sub cmdClose_Click()
CloseForm(YourFormName)
End sub
Public Sub CloseForm(ByVal strFormName As String)
Dim iCounter As Integer
For Each frm In Forms
With frm
If (.Name = strFormName) Then
iCounter = iCounter + 1
End If
End With
Next
If (iCounter > 0) Then
For i = 1 To iCounter
DoCmd.Close acForm, strFormName, acSaveNo
Next
End If
End Sub