I am trying to get the Dlookup function to work in Access 2013, but i just cannot get it to work, heres what i have so far :-
I have a query called qry_VehicleOverview in this query there are Two fields called VehicleNumber and DateLastExam
I have a form, there are a number of fields, two of them called Vehicle1 and DateLastExamV1, in DateLastExam1 ! am trying to reference the relevant exam based on Vehicle1 from qry_VehicleOverview field, so when a Vehicle Number is added to Vehicle1 it displays the correct Exam in DateLastExam1
first of all I create a combo box in the form, called Vehicle1 and referenced it to VehicleNumber from qry_VehicleOverview
then i created a text box in the form, and called it LastExamVehicle1, in the control source of this field I added the DLookup function :-
=DLookup("[DateLastExam]","qry_VehicleOverview","[VehicleNumber]=""" & [Vehicle1].[Text] & """")
Then chose After update in Event tab and Selected code Builder in here I added :-
Private Sub LastExamVehicle1_AfterUpdate()
Me.LastExamVehicle1.Requery
End Sub
but when run the form, first of all i get an error of #Type in the field, when i change the value in Vehicle1 the eror then changes to #Error
I create a combo box in the form, called Vehicle1 and referenced it to
VehicleNumber from qry_VehicleOverview
If that is so, there is no need for DLookup as you already have the value.
Set the RowSource of Vehicle1 to qry_VehicleOverview and the count of fields for the combobox to 2 and use this ControlSource for your textbox:
=[Vehicle1].[Column](1)
It will automatically update.
Don't use [Vehicle1].[Text], use [Vehicle1].[Value] instead. Or just [Vehicle1] .
(.Value is the default property)
.Text is only valid while the focus is in the control, and in AfterUpdate it isn't anymore.
.Text is mainly useful in the Change event, but that wouldn't make sense for your case.
Edit
You probably need the "full path" to the control in DLookup. For better readability I suggest using single quotes.
=DLookup("[DateLastExam]","qry_VehicleOverview","[VehicleNumber]='" & Forms!yourForm![Vehicle1] & "'")
Also please double-check all names in your form (and then in your question). E.g. from your description, your event procedure should read:
Private Sub Vehicle1_AfterUpdate()
Me.DateLastExamV1.Requery
End Sub
Related
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 am trying to filter a subform with a textbox.
I have a query to show table records in the subform and I have the criteria to filter the table, but when I type in the textbox to filter the sub form it only shows me one record with that name. I need it to show me all the names.
The criteria for my query is below.
Like "*" & [Forms]![frmPlanningForecast]![FETextbox].[Text] & "*"
I then have an OnChange event on the textbox to requery the subform.
As mentioned above I need it to show me all the records matching what i have typed, not just one.
When I use the filter option within the table itself(Dropdown box on the field header) and select the filter from there, it works great. But I need it to be typed into a textbox.
The picture attached will show you what I mean, I have "EQ" typed in the text box but it has only returned 1 record when their are 15 called "EQ" on the table.
First of all add single quotes around Like parameter:
Like "'*" & [Forms]![frmPlanningForecast]![FETextbox] & "*'"
and second - Access has an old bug: if you refer in the query to form field like you did, it doesn't renew the value, used for criteria during Requery, you always will receive the same results. Workaround - replace reference to field by global function, which returns textbox value or use dynamic generating of RecordSource for subform.
Global function example:
Public Function GetFE() As Variable
GetFE = [Forms]![frmPlanningForecast]![FETextbox]
End Function
Place it in any standard VBA module. Then your Like will look like this:
Like "'*" & GetFE() & "*'"
I found an easier method and just wanted to let others know.
Instead of using criteria I used the following vba code.
DoCmd.ApplyFilter , (FETextbox = qryPlannedHours.LeadFE), SubFormPF
In older versions of Access, didn't there used to be an option in the query to use the sum field something like this:
[Formnane].[TextField]
I know that's very simplistic and I don't fully recall how to use it but it was something like that, simple and straight forward if you're not a VB user. Forgive my lack of conviction, I've used it before but it was 20 years ago.
I'm trying to replicate a feature in the Access 2007 "Issues" template/example database. When you open up the 'Issues List' form, and click on an ID, it behaves like a hyperlink and opens up that record in another form.
How can I replicate this? I'm not a big fan of using the Access 'create macro' feature and would prefer to use the VBA editor if possible.
Thanks in advance for your help.
I didn't examine that template database, but your description sounds like something you can handle with DoCmd.OpenForm.
Say your form includes a text box named txtID which is bound to a numeric field, ID, in the form's record source. Create a VBA procedure for the text box's click event to pass the current ID value as the OpenForm WhereCondition parameter. (This assumes the record source of the next form also includes that numeric ID field.)
Private Sub txtID_Click()
Const cstrForm As String = "YourNextFormName" ' <-- change this
DoCmd.OpenForm cstrForm, WhereCondition:="[ID]=" & Me.txtID
End Sub
If the data type of the ID field is text rather than numeric, include quotes around the value in the WhereCondition.
WhereCondition:="[ID]='" & Me.txtID & "'"
What you want to do is first set the text field you're interested to a hyperlink. You do this by setting Display As Hyperlink in the Format tab of the Property Sheet or you can do this via vba as following (although it seems to be bugged visually i.e. clickable link but not displayed as such)
myTextBox.DisplayAsHyperlink = acDisplayAsHyperlinkOnScreenOnly
After you do that, create a Click event. In the subroutine you can run the code to open your form filtered with the record number (or appropriate argument)
DoCmd.OpenForm "myFormName", acNormal, , "[ID]=" & Nz([Id], 0)
I am working on forms and made a single form for data entry and search ( data extraction) but the problem is that i used Dlookup formula on some textboxes for ease in data entry but when i attemp to search access doesn't show data on that textbox and shows the error that the object is read only.
How can i get the textbox show data as well as have Dlookup formula?
Kindly help.
Many thanx
You can set the textbox value by code instead of putting the DLookup into the data source of the textbox.
Putting it into the data source means that you can't edit the textbox at runtime, as you experienced.
But you can set the value once in the Form_Open event, for example:
Private Sub Form_Open(Cancel As Integer)
Me.TheTextBox = DLookup(...)
End Sub
This will cause the textbox to be filled automatically when the form opens, but the textbox is editable and you can overwrite the value.
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.