VBA how to enable a Click Event in a subform? - vba

I have a Subform, which shows the results of a search, using Data of many searchboxes. I'd like to use a subform instead of a ListBox, because so the user can use the OrderBy and filters. Now I want that the user can doubleclick a field, and receive the first value of this row.
Me.SearchResultForm.Form.RecordSource = query 'query contains the precalculated sql query
Me.SearchResultForm.Requery
This works really well.
Me.SearchResultForm.Form.OnDblClick = onResultDblClick '--> a function
This does something strange: It fires once at requery, but NOT at doubleclick...
How can I get this working? Or is there a better solution?
Thanks for your help!

Try the following:
Me.SearchResultForm.Form.OnDblClick = "=onResultDblClick()" '--> a function
As noted on msdn here, when setting an Event Property, you assign it a string that is either a Macro, an Event Procedure or a User Defined Function.

Related

MS Access: How to bound text box of form with query?

I created a form in MS Access 2010 and added a textbox here. Then I created a simple query (for example SELECT 10 AS studval;) and tried to set in Properties (of textbox) -> Data -> Control Source this query, but I got error #Name?.
How do I fix this error?
All names of query, textbox, query return values are correct. Or maybe are there any other ways to bound textbox and custom SQL query?
There is no easy way to do it, but it is possible using the form's On Activate event. First set up a query (Query1) with a single value called "studval" then open the form properties and add an Event Procedure for On Activate. It should look like this:
Private Sub Form_Activate()
Dim myString As String
myString = CurrentDb.QueryDefs("Query1").OpenRecordset.Fields("studval")
Me.Text0.SetFocus
[Text0].Text = myString
End Sub
You need to set the Control Source of the form to the query rather than the control source of the text box. A text box control source can only refer back to it's form's control source.
If you want just one text box bound to a query you have to create a subform linked to the parent form with that text box in it.

Microsoft Access query based on option button

I am building a query in Access. One of the fields [V2] needs to be filtered if an option button on the [Main] form is selected. Right now, I have the following criteria entered for the field.
Field: Expr3: [V2]>0
On Criteria: IIf([FORMS]![Main]![optV2]<0,True)
In the event that the option button is selected, the data is sorted properly. However, in the event that [FORMS]![Main]![optV2] is not <0 (meaning the option button is not selected), I would like all data regardless of [V2] value to be displayed. Currently, no data is displayed if the button is not selected.
Any suggestions on how to do this?
Any help is appreciated!
Thanks,
Lucas
You need to get rid of the iif function surrounding your criterion. It needs to look like this:
Criteria: [Forms]![Main]![optV2] < 0
or like this:
Criteria: [Forms]![Main]![optV2] = true
The reason your way is not working is you don't specify, in your iif function, what value to return if the condition evaluates to false. Strictly speaking, the correct form of your function call would have been:
iif([Forms]![Main]![optV2] < 0, true, false)
But since the query designer criteria entry field already evaluates the condition, you don't need the iif function there at all.

Access DoCmd.OpenForm Not Working

Looking for a second set of eyes to figure out my problem with an Access form filter. I created a search form, when filled in, appends search criteria to a string variable (strQuery) that is put in place to the [WhereCondition] for opening a form. However, when the script is ran, nothing comes up except for a filtered form with no records.
Here is the line that opens the form:
DoCmd.OpenForm "ADD_NEW_NCMR", , , strQuery
Before the line is ran, strQuery equals:
1=1 AND [NCMR].[NCMR_NUM] = '12-129'
The form name, and table.column combination are all correct. In fact, using the DCount function returns the result of 1, which is correct for this query, and returns the correct number for other queries as well. This makes me think that there is nothing wrong with the where condition.
DCount("[NCMR_NUM]", "NCMR", strQuery)
Check your form's Data Entry property. You can find it on the Data tab of the form's property sheet.
If Data Entry = Yes, the form will not display existing records.
Sounds like you want Data Entry = No, so that existing records which match your OpenForm WhereCondition will be displayed.

Gray out a form row's (detail's) button conditionally with VBA code

I have a standard form in MS-Access which lists a bunch of orders, and each row contains order no, customer, etc fields + a button to view notes and attached document files.
On request from our customer we should gray out the button btnAnm (or check or uncheck a checkbox) depending on a calculation from two queries to two other tables (a SELECT COUNT WHERE and a check if a text field is empty).
I've tried btnAnm_BeforeUpdate(...) and btnAnm_BeforeRender(...) and put breakpoints in the subs, but none of them trigger. The same if I use the control Ordernr instead of btnAnm.
I'd like a function in the Detail VBA code to be triggered for each "Me." (row) so to speak, and set the row's control's properties in that sub.
What do I do? I've looked at the help file and searched here.
*Edit: So I want to do something that "isn't made to work that way"? Ie. events are not triggered in Details.
As an alternative, could I base the value of a checkbox on each line on a query based on the 'Ordernr' field of the current row and the result of a SELECT COUNT from another table and empty field check?
Do I do this in the query the list is based on, or can I bind the extra checkbox field to a query?
A description of how to do this (combine a COUNT and a WHERE "not empty" to yes/no checkbox value) would be perfectly acceptable, I think! :)*
You cannot do much with an unbound control in a continuous form, anything you do will only apply to the current record. You can use a bound control with a click event so that it acts like a button.
Presumably the related documents have a reference to the order number that appears on your form, which means that you can create a control, let us call it CountOrders, with a ControlSource like so:
=DCount("OrderID","QueryName","OrderID=" & [OrderID])
The control can be hidden, or you can set it up to return true or False for use with a textbox, you can also use it for Conditional Formatting, but sadly, not for command buttons.
Expression Is [CountOrders]>0
You can also hide the contents and add a click event so that is acts in place of the command button. Conditional Formatting will allow you to enable or disable a textbox.
As I understand your question, you have a continuous form with as command button that appears on each row - and you'd like to enable/disable the button conditionally depending on the contents of the row.
Unfortunately you can't do that. It seems that you can't reference the individual command buttons separately.
Having wanted to do something similar in the past I came up with two alternate ways of setting up my interface.
Put a trap into the onClick code for the Button. Which is icky, because it is counter intuitive to the user. But it gets you that functionality now.
Move the command button (and editable fields) up into the form header, and make the rows read only. Your users then interact with the record only in the header, and select the record they want work with in the list below. As I recall this is known a Master-Detail interface.

Populate list box from a table in vba

I am developing a vba form for employee database, in that there is a search criteria for userid and employees name with the userid should be displayed in a list box control which comes from a single table
I need to populate a list box with value from a table according to a value in a text box which act as a search box (eg: userid)
Please help me how to do this
You question is hard to answer because it depends on some things like data types of the search field etc. So this answer is going to be vague on some of those points...
First off you need to create your listbox with search criteria that will look on the form for the search value and filter accordingly.
You do this by setting the the RowSource property of the listbox. HEre is an example rowsource for a a listbox that looks for a textbox on a form for its filter value...
SELECT tblAgencies.AgencyID, tblAgencies.OrganizationName
FROM tblAgencies
WHERE (((tblAgencies.OrganizationName)
Like "*" & nz([Forms]![frmMainMenu2]![txtSearchAgencies],"") & "*"))
ORDER BY tblAgencies.OrganizationName;
The key part is the Like... line. A couple of things about it...notice that the query looks to the form for some criteria. You see that in the [Forms]![frmMainMenu2]![txtSearchAgencies] part of the query. So there is a search textbox on frmMainMenu2 that is called txtSearchAgencies.
Notice also that I am using NZ function to ensure that the peek onto that textbox returns at least an empty string. Finally notice that is uses the Like operator with wild cards at both ends so that the user can type a partial string.
Finally...next to your search box put a command button to execute the filter/search. All that it has to do is REQUERY the listbox like this...
Me.lstAgencies.Requery.
You could also try to Requery at the OnChange event which would filter as they type. But if your query is slow this may not work well.
Seth
Let's say you have a table TABLE1 that has fields userid, employee.
You should create a form that has a combobox(named boxid) and textbox(named EdtEmployee).
Define a rowsource value of combobox like
SELECT table1.userid FROM table1 WHERE employee like EdtEmployee & "*";
Then define a lostfocus event of a textbox like this
Private Sub EdtEmployee_LostFocus()
BoxId.Requery
End Sub
I hope, this works for you
I agree with mik.
I just would use an AfterUpdate event instead of the LostFocus which I've neber used.