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
Related
I've got an Access Form with a combobox bound to a query that selects 2 fields. I managed to get the combobox showing up the query's second column by setting up in the combobox's Property Sheet the following: Column Number = 2 (with the first column width = 0); Bound Column = 1 (because it contains a value input for another query).
My problem is that when I open the Form, choose a value from the combobox and then save the Form as a Report, the combobox goes blank! In the Report I don't find the value previously selected for the combobox in the Form.
How can I fix it?
It appears you are looking to give the end user the ability to print what they see on the data entry form. But in access the printing functions are attached to reports. So we need a report that looks like the data entry form and some way to access the report from the data entry form. So add a print button or some such that will open the report. The report needs to look like the form so saving the form as a report is a good jump start but then some comboboxes don't show. Skip to the end for an explanation why. So we have to set the problem report comboboxes manually. One way is to pass parameters to the report when it opens:
Private Sub PrintButton_Click()
'look up DoCmd.OpenReport if you want to do something other than print preview
DoCmd.OpenReport ReportName:="ReportSavedFromForm", OpenArgs:=OriginalformComboBox.Value, View:=acViewPreview
End Sub
Private Sub ReportSavedFromForm_Load()
reportCombobox = Me.OpenArgs
End Sub
As to why the combobox is blank or stuck on the first value, according to the following link reports are not supposed to be used for editing data.
https://social.msdn.microsoft.com/Forums/office/en-US/14c6ec9a-53bd-4546-ba0e-597c41ca7cce/combo-box-drop-down-arrow-invisible-on-reports?forum=accessdev\
So the combobox dropdown arrow will not appear by design. I put this to the test in office 2016 and in the report header and report footer sections the combobox appears blank if the combobox is unbound. If the combobox is bound say to an ID the combobox behaves a little differently. It shows the first record but is just a textbox with no drop down arrow so only the first record ever shows. However in the details section while you still don't have the dropdown arrow the combo box still can be used to replace an id with a more friendly value
Good Day all. I have a quick question. I have an openargs value that I'm am trying to use to show up in my combobox (cmbMemberName) on return from another form. The combobox populate the underlining subform. I just can't seem to get the right method. I can't use recordsource cause that would filter out the rest of the records. Rem: I just want the focus on the updated record and loaded into the combobox upon return. Here's the last method I tried.
If Nz(Me.OpenArgs) <> 0 Then
Me.cmbMemberName.SetFocus
DoCmd.FindRecord Me.OpenArgs
MsgBox (Me.OpenArgs)
Me!cmbMemberName.Dropdown
Else
....
The error occurs on the DoCmd. Any suggestions. Thanks. I could load the entire sequence, but don't think that would be necessary.
First you have to set the value of the combo box. Assuming your openargs matches the bound column of the combo box that should simply be
Me.cmbMemberName = Me.OpenArgs
After that you need to get you subform to populate based on the combo box value. Assuming you have set the subform to properly read the value you just need to requery it
Me.MySubForm.requery
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.
I have created on a database a Dlookup function to change the email address of a manager whenever the managers area is selected. It works fine but now that the control source is the Dlookup it doesn't save the results any more in the personal table.
I read up on http://p2p.wrox.com/access-vba/77907-how-save-results-dlookup-function.html a method to have a separate hidden box which displays the results from the table, which works but my trouble is now connecting the Dlookup result to the other text box.
I obviously cant control source the Dlookup result so I've instead tried to make it an before update event using the following code;
Option Compare Database
Private Sub ASMail_AfterUpdate()
ASMEmail.Value = ASMail.Value
End Sub
However this has not taken effect at all. The textbox does not change whenever I adjust the Dlookup results, and I've tried the same code in the other Change event which didn't work either.
You don't need a separate hidden textbox.
Leave the textbox control source of the manager's email linked to the personal table field and simply update the textbox value to the DLookup value when the manager area is selected.
I don't know how the manager area is selected, but as a combobox example, it would be like this:
Private Sub Combo_AfterUpdate()
Me.ASMEmail.Value = Nz(DLookup("Value", "Domain", "Criteria"), vbNullString)
End Sub
I have a database with information about visitors to our department. Here is my setup: A user selects a visitor name from a listbox. Access assigns the values from that visitor's record to temporary variables and opens an unbound form where those variables are the default values in the text field. When a user changes a value in a textbox, an After_Update event uses SQL to update the corresponding field in that visitor's record and assigns the new value to the temporary variable.
After_Update example:
Dim strRUN As String
strRUN = updateVisitorOnDirty(Me.txtVisitorTravelMethod, "VisitorTravelMethod", 1)
HideWarnings (strRUN)
TempVars!strVisitorTravelMethod = Nz(Me.txtVisitorTravelMethod.Value, "")
"updateVisitorOnDirty" is a function that returns SQL to update a field (specified in second argument) in the table with visitor information with the first argument. (The number at the end is says it's a string.) "HideWarnings" turns warnings off, executes the SQL, and then turns warnings back on.
My problem:
When a user clears a textbox, the After_Update event makes the textbox revert to it's previous value.
I thought at first that maybe the unbound textbox was reverting to its default value when it was cleared, but I put a breakpoint in the sub that fires during After_Update:
If I change a value in mytextbox from a to b, debug.print shows the value of mytextbox as b. If I change a value in mytextbox from a to (blank, or empty string), debug.print shows the value of mytextbox to still equal a.
My workaround has been to include a little "x" near the textbox that the user can click to 1)clear the textbox, 2)update the temporary variable, and 3)update the table. It works fine, but it seems like there should be a better way. Help?
Please let me know if you need any other information or if I should otherwise re-word this. I come here all the time for answers, but this is the first time I've actually submitted my own question. Thank you!
(I edited this to make the code show up correctly...didn't leave a line before.)
I figured out that the unbound textboxes were reverting to their default value when a user tried to make them blank. To stop them from doing this, I put this in the AfterUpdate of the textboxes:
Call ResetDefault(Me.textbox1.Text, Me.textbox1)
And defined the function:
Public Sub ResetDefault(ByVal strChange As String, ByRef txtCurrent As TextBox)
If Nz(strChange, "") = "" Then txtCurrent.DefaultValue = ""
End Sub
That is, if a user cleared a textbox, the default value of that textbox was set to "", allowing the textbox to actually go blank.
I think the best approach is to scrap the after_update event on your text boxes and instead either put a save button on your unbound form so that all updates are written in one pass, or capture the forms closing event and update your table then.
This is the advantage of using unbound forms, you do not have to comit data until you are absolutely ready to, but with using an after_update event you are almost mimic-ing the behaviour of a bound form.