MSAccess OnClick Triggers stop working after changing RecordSource - ms-access

I use some vba script in a secondary form to blank the recordsource of the primary form, recreate the source table, and then set the recordsource back as the replaced table.
Forms("mainfrm").RecordSource = ""
DoCmd.OpenQuery "Create_Customers_Main"
Forms("mainfrm").RecordSource = "select * from Local_Customers_Main"
This disables the OnClick events on the primary form until a blank area of the form has been clicked.
Does anyone have a work around for this problem?
continue to flourish,
Phillip

Related

Setting Form recordsource using DoCmd.Openform

I have a single form which can be accessed from multiple places in my Access 2013 app - depending on where the form is opened from, the recordsource should be different - for example if opening the form to search for a record, the form recordsource is a parameter query where the user enters the record id as the parameter - but if opening the form from elsewhere the recordsource is a select query and the record id is passed using VBA.
I want to avoid having twin forms if possible - I just want a single form and be able to set the recordsource when the form opens... is it possible to set the form recordsource upon opening the form (rather than after the form is opened)?
Have a bit of VBA that rewrites the query that your form reads from. Try this:
Set db = CurrentDb()
DoCmd.DeleteObject acQuery, "myFormQuery"
Set q = db.CreateQueryDef("myFormQuery")
q.Sql = "SELECT * FROM mytable WHERE 1=1;"
DoCmd.OpenForm "myForm", acNormal

How to partially clear userform in ms access

I have a bound userform with lot of fields in it, I use submit button to let user make entries in shared form. But whenever user clicks on submit button it requery whole form. However, there are around 4-5 fields which should not be cleared on submit button, they should retain the value and rest of the fields should get cleared on every submit button click.
Can this be done in access and how ? below is the simple code is use to submit and requery the record.
Private Sub SaveBtn_Click()
RunCommand acCmdSaveRecord
Me.Requery
Me.DataForm.Requery
End Sub
When you have a "Bound Form" in Access, this means that the Access Form is tied to the underlying data source, which is specified by, and stored in, the "Record Source" property for the form. In the "SaveBtn_Click()" method you provided above, this code does the following things:
RunCommand acCmdSaveRecord - Saves the current record in the form.
Me.Requery - Requeries the entire form.
Me.DataForm.Requery - Requeries the subform named "DataForm" on your main form.
So when you "Requery" the entire form (depending on how the "Record Source" and other form property settings are setup), the requery operation in some cases moves the cursor to the new record in the data source (...its the default settings for the drag and drop form designer tools in later versions of Access), and I suspect that is why you see the form being "cleared" when you call "Requery." A couple of items I would point out, just for clarity:
Note 1: "Requery" actually saves the record, so explicitly calling "RunCommand acCmdSaveRecord" should not be necessary if you're going to call "Requery" too.
Note 2: Depending on how the Record Source (and other) form properties are set, Requery in some cases just saves and refreshes the currently selected record, but it doesn't sound like that is how your form is working (based on what you said above), so I'm assuming that is not the case in your form.
Note 3: The subform will also be requeried when you call "Requery" for the main form, so that line of code may also be redundant here. The reason to call "Me.DataForm.Requery" is if you only want to requery the subform and NOT requery the entire main form.
Note 4: The "DataForm" subform (on your main form) will have a separate data source for it's own "Record Source" property, which is separate from the parent (main) form, so it is important to be aware of that fact, depending on which field values you want to save.
So, that said, there are a couple of options I might suggest, depending on exactly how you want your form to behave:
If you want to keep some of the field values and use those for the NEW RECORD when you hit the requery button, you could always save them off in a variable and then set those controls again after requerying from your variables. Just create a seperate variable for each value you want to save, copy the TextBox control values into each of variables respectively, call requery on the form, and then copy those values back into your TextBox controls after you requery. That code would be something like the following (depending on the exact names of your TextBox controls, I used the fictitious name "txtMyTextBox" for this example):
Private Sub SaveBtn_Click()
Dim vValue1 as Variant
vValue1 = txtMyTextBox
Me.Requery
txtMyTextBox = vValue1
End Sub
Or, if you're just trying to create "Default Values" for certain controls, TextBox controls have a "DefaultValue" property you can set to any value you would like to use for the default. Those can be set from the Property Sheet on the right side of the Access window when the form is opened in Design mode or Layout mode and the TextBox control is selected. (Press F4 to open the Property Sheet if it's not already open).
But, if you really want to Requery and then go back to the same record you were previously on, you could try the following code:
Private Sub SaveBtn_Click()
Dim iRecord as Integer
iRecord = Me.CurrentRecord
Me.Requery
DoCmd.GoToRecord , , acGoTo, iRecord
End Sub
Anyway, I hope this all makes sense and you find it helpful. Please let me know if you have any further questions about this answer.

Access - Do Not Query Subform When Parentform is Opened

I have an MS Access form, that contains a subform whose recordset is a query that takes quite a while to process and isn't actually needed right away when the parentform is opened, but only after a button on the form is pressed.
Is there a way to implement this with VBA other then completely changing the form with something like Set Me.MySubForm.Form.Recordset = NULL? I've tried Me.MySubForm.Enabled = False but that doesn't work unfortunately.
The easiest way:
Leave the SourceObject property of the subform control empty. Then when the button is pressed, do
Me.MySubForm.SourceObject = "mySubFormName"
This way the subform and its data isn't loaded until needed.

Editing only filtered entries in an Access form using VBA

I have been adding on to the Access contact template to create a database which can manage contacts and the main form that opens up has a datasheet filled with all the contact info. I check-box functionality to each item. If this box is checked, you can run certain actions on all the checked box items. I have created a macro that can select/deselect all of contacts at once.
But what I am trying to do is enable it so that when you click the button, it selects only the items that are currently shown. So if I apply a filter in the form for lets say Zip Code, I can hit the button that will change the "Action" field to "Yes" so that I can run the actions on it (bulk send email, export all to outlook, create address labels, delete, etc.).
In Excel this could be done with a .visible type thing, but there doesn't seem to be a function like that for access. I have done all my other methods through creating a recordset of the data, but this recordset won't have the same filters.
Let me know if there is any other information I can provide.
To set the value of a checkbox, you can either use 0 or -1. -1 for checked and 0 for unchecked in vba.
Me.checkboxname.Value = -1
now what you can do is select all the checkboxes in your form, create a function or a sub for onclick event from the button and add this code that should check all boxes:
'code not tested but should work fine
Dim ctrl As Control '//, chbox As CheckBox
For Each ctrl In DAO.Controls
If Typeof ctrl Is MSForms.CheckBox Then
'// Set chbox = ctrl
Me.ctrl.value = -1
End If
End If
Next ctrl

Allow input on an auto-number field in form to create new record in Access

I have a form that will display the last record of a table, with a few additional fields from other tables. The PK of the main table is auto-numbering. I need to be able to allow the user to make a change to the auto-numbering field, then determine if the record exists or not, if so - update, if not - insert new. I tried adding an event to the field on field change but whenever I try to click in the field on the form, it just beeps at me. Here is the code:
Private Sub JobID_Change()
'Check Bid#, if already exists open selected record for editing
rstOpenOrder.FindFirst "JobID = " & Me![JobID]
Do Until rstOpenOrder.NoMatch
With rstOpenOrder
'Add new record to end of Recordset object.
.Edit
'Edit data.
!LocationID = Me![LocationID]
!Description = Me![JobName]
!BaseBid = Me![BaseBid]
!GrossMargin = Me![GrossMargin]
!MDs = Me![ManDays]
!BidDate = Me![BidDate]
!ShortDate = Me![ShortDate]
!EmployeeID = Me![EmployeeID]
!GC = Me![GCs]
.Update
.FindNext "JobID = " & Me![JobID]
'Save changes.
End With
Loop
End Sub
If anyone can help, I would greatly appreciate it!
It's not generally a good idea to expose autonumber fields in the user interface. However, if you still want to provide the interface you've described you'll need to do the following:
Add an unbound text box (I'll call it IDLookup)
Add an AfterUpdate event to the text box to check for an existing record
If the record exists, filter the form to display that one record
If not, move the form to a new record (Me.Recordset.AddNew)
The key here is that your lookup must be done via an unbound text box. Access won't let you edit anything inside a textbox bound to an AutoNumber field (for good reason!).