MS Access NOT In List Event Work-around? - ms-access

I have a form with an unbound combo "search" and a command button "close form". When someone types a value into the search combo, and the value is not found in the search, it triggers the not in list event, so far so good.
My challenge is this: if the person types the value into the search combo, sees that no records are returned (in the dropdown of the combo), then goes to click the command button "close form", the not in list event still fires with the error message even though they have already clicked the "close" button.
When stepping through the code you see that even after the click of the command button the active control is still the combo box because it's trying to finish the not in list event.
Access doesn't even know the command button was clicked, all it knows is that the combo lost focus... so in order to close the form, they have to click the command button twice.
Here is my existing code-
Private Sub cboItemSearch_NotInList(NewData As String, Response As Integer)
Me.cboItemSearch = ""
Response = acDataErrContinue
End Sub
Does anyone have a work-around for this? Thanks in advance!

Related

how can my ms access cancel-button call the same event/code as the X in the corner?

I have an MS Access database/application.
I have a form on which I create a new record.
When the user chooses the X in the corner of the window, then this message is shown:
You can't save this record at this time.
[form-title] may have encountered an error while trying to save a record.
If you close this object now, the data changes you made will be lost.
Do you want to close the database object anyway?"
On that form, I also have a cancel button with the VBA code DoCmd.Close
When the user chooses the cancel button, then the above message is not shown...
What can I do, so that the above message is shown, when the user chooses the cancel button?
In other words: I want the cancel-button to call the same event/code as the X.
thanks

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

DCOUNT result not being passed to tab control caption (error)

I've create a form with a tab control in MS-Access.
On one of the tabs I have 2 subform controls showing records from the same table. This table has a field I've called "Status" and can have an entry of either "Open" or "Closed"; the 2 subforms simply show these 2 options filtered as separate groups on the tab control.
I then have a button underneath each of the subforms; the user selects (clicks into) one of the records in either of the subforms and then the button will open a new "Edit" form at the selected record in subform so they can see more information and also toggle the "Status" field to "Open" or "Closed".
When the user presses the "Save and Close" button this saves the changes, closes the "Edit" form, and re-queries the 2 subforms.
What I'm trying to do is get a count of the number "Open" records still showing after the "Edit" form is closed. The count number is then to be passed in to the tab caption for this tab so users can see at a glance how many "open" records are against the record on this main form.
Here's my code so far:
Private Sub cmdAdminIssue_Edit_SaveClose_Click()
' save and close currently open edit form
DoCmd.Close acForm, "tblAdminIssue_Edit", acSaveYes
' requery the subforms to show the effect of the edits made via the edit form
Forms![tblJobs]![tblAdminIssue_Sub_Open].Form.Requery
Forms![tblJobs]![tblAdminIssue_Sub_Closed].Form.Requery
' count number of records still open and pass the number through to the tab control caption property
Dim AdIssOpenCount As Long
AdIssOpenCount = DCount("JobID", "qryAdminIssue_Open", "JobID = '" & Me![JobID] & "'")
Forms![tblJobs]![tab_AdIssues].Caption = "Admin Issues (" & AdIssOpenCount & ")"
End Sub
The above code triggers the error: "The expression you entered refers to an object that is closed or doesn't exist", highlighting my DCount expression in debug.
However I can get the DCount to work independently of the other code above it; they seem to be interfering with each other, but I don't quite understand how.
Referring to the error message above it seems to be suggesting that DCount needs the query I referenced to open in order to run... this doesn't make much sense to me as I thought these kinds of functions do not require you to explicitly open in code an object it's trying to get data from... I'm probably misinterpreting what this error actually means though.
Any explanation of the error and a possible workaround would be much appreciated. Thanks.
While you've clearly done your best to explain the situation it seems pretty complicated.
I'm going to try and break it down a bit, and suggest something you might do.
"I've create a form with a tab control in MS-Access.
On one of the tabs I have 2 subform controls showing records from the same table.
This table has a field I've called "Status" and can have an entry of either "Open"
or "Closed"; the 2 subforms simply show these 2 options filtered as separate groups on
the tab control."
So, you have a form, with 2 subforms. That they're on a tab page shouldnt really be relevant. I'm going to reference the form as frm & the subforms as sf_o & sf_c.
Button underneath each subform (opening the detail form with a double-click might be nicer) - the "save & close" button is presumably in the popup screen; This button is currently performing the save of it's own data, and doing everything else as well.
This is very messy, as it means that the contents of frm cannot be modified without having to make changes to the logic of the save/close button as well. A better approach would be to separate the logic for refreshing the main form from the editting form using a public method in the main form (frm):
Public Sub RefreshData()
sf_o.Form.Requery
sf_c.Form.Requery
'other logic
End Sub
then, you have:
Private sub cmdAdminIssue_Edit_SaveClose_Click()
'save & close logic...
Forms(frm).RefreshData
End sub
Okay now, the other logic. You want to find out how many records are now "Open" and put this information somewhere. In this case, somewhere is a tabpage caption, but this isnt really material.
I never use DCount (DLookup etc) as it is very slow. An alternative (and in this case, much simpler) method would be to use the forms own properties:
tab_AdIssues.Caption = "Admin Issues (" & trim(sf_o.Form.RecordsetClone.RecordCount) & ")"
How's that? Then you dont need to try and figure out what's gone wrong in all that pre-compiled logic which is microsoft access.
Hope this helps

MS Access how can a subform get the value from another subform?

I have a main form that contains two other child subforms. When a textbox value changes on subform (A) I would like to set the value of a textbox on subform (B) equal to the changed textbox on subform (A).
So being completely new to Access I did some googling and it looks like this can accomplish this with some VBA. So on subform (A)'s textbox 'after update' event I have added this VBA.
Private Sub ID_AfterUpdate()
Me.Parent.Forms.formA.textboxDestination.Value = Me.textboxSource.Value
End Sub
I am doing this using the code builder. Then I save and run the form. Nothing happens. No errors.. nothing I am not even sure that piece of code is running. I'm not even sure how one debugs VBA in Access. Can someone help me out?
Thanks!
The after update event for a control fires when the user changes its value. If ID is bound to an autonumber field, the db engine will supply its value when you add a new record. However, since the user didn't make that change, the after update event does not fire.
For general debugging purposes, you can add a Debug.Print or a MsgBox statement.
Debug.Print "my event fired"
MsgBox "my event fired"
View the output from Debug.Print in the Immediate Window of the VB Editor. You can go there from your main Access window with the Ctrl+g keyboard shortcut.
Another technique is to set a break point on a line of your code. Right-click on the code line, then choose Toggle->Breakpoint from the context menu. Or click in the left margin so that a reddish dot appears to mark the breakpoint. Or press the F9 key. Or choose Debug->Toggle Breakpoint from the VB Editor's main menu.
You can also type Stop on a line by itself to trigger break mode. However, you would need to remove it later. Those other breakpoints I mentioned are temporary and don't get saved in the code module.
However you get to break mode, you can then step through the code one line at a time with the F8 key. That will show you which lines are executed. You could also inspect the value of a variable at any time in break mode by typing a question mark, then variable name, and then the Enter key in the Immediate Window:
? MyVariable
Explore the VB Editor's main menu to find additional debugging options. For example, the Watch Window will allow you to monitor the values of selected variables as you step through the code.
You don't have to specify the name of the parent form.
Me.Parent.CONTROLNAME.Value is a kind of working code.
Regards
On the update property of the textbox you are changing, put the following:
[Forms]![frmContainingValueYouWantToChange]![txtTextboxYouWantToChange] = "newValueHere"
Let us know how you get on?
I know it was 6 years ago, but I think the error here was that you were updating value from the formA to the same formA.
Me.Parent.Forms.formA actually igual to Me because you were calling :
subform (A)'s textbox 'after update' event
It should be formB and the word "Form" should be placed after (and the word "Value" isn't mandatory):
Private Sub textboxSource_AfterUpdate()
Me.Parent.formB.Form.textboxDestination.Value = Me.textboxSource.Value
End Sub
You can learn here some more informations how to Refer to Form and Subform properties and controls
Regards

Dropdown in Access 2007 parameter query

I want a Access parameter query to ask an user for a value (a location in this case). When I type [Enter location] in the Criteria field it works fine: I get a dialog box (Enter Parameter Value) with a textbox and my text (Enter Location). So far, so good. This works (the result also).
But now I want a dropdown/combobox (instead of a textbox ) for the user to pick a location. I made a form and type Forms![Form1]![CmbLocation] in the Criteria field.
Like this: http://office.microsoft.com/en-us/access/HA011170771033.aspx
But I still get a textbox (with the reference as textlabel).
What am I doing wrong? Has anybody any advice?
In addition to Albert's suggestion, you might want to make this work within the query itself, so that it's "bootstrappable." To do that, you'd have to write function that returns the value chosen in the combo box on the form. It would be something like this:
Public Function ReturnMyCriterion() As Variant
DoCmd.OpenForm "dlgGetCriterion", , , , , acDialog
With Forms!dlgGetCriterion
If .Tag <> "Cancel" Then
ReturnMyCriterion = Nz(!cmbMyCombo, "*")
End If
Else
ReturnMyCriterion = "*"
End With
Close acForm, "dlgGetCriterion"
End Function
(when opening a form with the acDialog switch, the code pauses as long as the form is open or visible; to get the value out of the combo box, you have to set the form's .Visible property to False. You could do this in the AfterUpdate event of the combo box, or in the OK button. You'd also want a Cancel button that set's the form's .Tag property to "Cancel" and then sets the form's .Visible property to False; this is all relatively a standard approach to working with dialog forms in Access).
You'd then make the criterion in your query be:
Like ReturnMyCriterion()
That is, assuming you want to return all records if no value is chosen in the combo box.
If you removed parameter form your query, and then re-typed in the above form exprsison into the query builder, then it should work.
So, in the query builder, in the criteria section just type in
[forms]![form1]![Combo4]
Make sure you have the right form name and control name of the combo box.
You should not need to type in anything else into the query builder. As mentoned, remove the old parameter prompt you previous had in the query builder.
Now, open the form, select the combo box, and now try opening the query, it should open without any prompts. Note this approach means that the form will have to be open, and the combo box will have be selected a value BEFORE you attempt to launch the query. So, if you basing a report on this query, then palce the button to launch the report on the same form as the one with combo box. This quite much ensures that the form will be open before you attempt to launch a query or report that is based on that query