Cannot reference to a form control which is out of focus - ms-access

This is a follow up to a previous question I asked about how to update a textbox while typing. In my previous question, I had three textboxes, two of which were enabled and one which was not. They look like this:
My goal is to use the First and Last Name of the two textboxes to fill a "Code Personal" in the other textbox. As I type the first and last names, I want the Code Personal textbox to update immediately, with the format of LLLLLL_F (eg: BERNAS_P). However, whenever I try to update the Code Personal textbox, I receive this error:
The code I use to create the Code Personal format and to update the textbox is:
Private Sub TxtFName_Change()
firstName = Me.TxtFName.Value
lastName = Me.txtLName.Value
firstPart = Left(lastName, 6)
secondPart = Left(firstName, 1)
nameCode = firstPart + "_" + secondPart
upperNameCode = UCase(nameCode)
txtCodePersonal.Text = upperNameCode 'My debug tells me I have an error here
End Sub
I've tried to set the focus to the txtCodePersonal textbox through: [txtCodePersonal].SetFocus, but I still receive an error (MS Access can't move the focus to the control txtCodePersonal.)
Any Ideas as to how I can update the "Code Personal" textbox while typing in the other two textboxes?
Thanks in advance for all your help.

Do your value assignment to the text box's .Value property instead of its .Text property. Either of these should work:
Me.txtCodePersonal.Value = upperNameCode
Me.txtCodePersonal = upperNameCode
.Value is the default property for a text box, so you don't need to include it explicitly. Include it if you feel it makes the code clearer.

Related

Hide/unhide combobox based on a text box

I have a form in access that works in this way:
User select an ID of a product
VBA code searches in the database and fill some textboxes in the form.
For example:
User select ID 1
Textbox 1 is filled with: fruit
My problem is, i have a combobox with options to select like "banana" or "apple" or "grape". This combobox is visible at the form, but i want it to be invisible and only appears if the textbox 1 is filled with "fruit".
In the design view, i set the combobox's visibility to "no", so it does not appear anymore in the form. Then, i wrote this code:
Private Sub textbox1_AfterUpdate()
If Me.textbox1 = "fruit" Then
Me.LabelOfTheCombobox.Visible = True
Me.Combobox.Visible = True
Else
Me.LabelOfTheCombobox.Visible = False
Me.Combobox.Visible = False
End if
End Sub
But it doesnt work, when the textbox is filled with "fruit", the combobox remains invisible.
Can you help me fix this code?
Everything looks OK. From what you have said, that putting "fruit" in the textbox doesn't cause the combobox to display, then my guess would be that the If-test is failing. Perhaps you could try trimming both sides of the textbox before the comparison. You should also pay attention to the case. Try forcing the case of the textbox to lower case to match the string constant.

Macro on Access to hide fields does not work

I am trying to figure out how to make a Macro work in Access but have never used it before. The program is telling me there is a type mismatch. I cannot figure out what is wrong with the way it is written, but there is certainly an issue because it does not do what I wish. What I am trying to do is make a field visible when a checkbox is checked and invisible when it is not.
My Macro page looks like this:
If [Forms]![Form name]![Checkbox]=True Then
Set Property
Control Name: Control
Property: Visible
Value:True
Place this code in the VBA editor (in "your form name" code):
Private Sub Checkbox1_Click()
If Me.Checkbox1 = True Then
Me.Control1.Visible = True
Else
Me.Control1.Visible = False
End If
End Sub
Remember to change "Checkbox1" and "Control1" to the appropriate controls names.

Autofilling my form in Access with the use of a Combo Box

I'm having a problem when I want to autofill my form in Microsoft Access. The idea is that I use a combo box to select a name. Then the onChange code of my Combobox automaticlly inserts all the other data in the proper field. I use this code on the Combo Box.
Private Sub cmbName_Change()
Me.tbPersonalNumber = Me.cmbName.Column(0)
Me.tbEmailadress = Me.cmbName.Column(2)
Me.tbBirthday = Me.cmbName.Column(3)
End Sub
This methode works fine for the personalnumber and the emailadress. But it doesn't work for the birthday date, it returns a null value but when I check my table there is is a date in the proper field.
Am I missing something? I tried everything but it wont work.
I was thinking that the problem is related to the birthday column being the last in the table. Or having the date type.
Thank you in advance for your time and efford!
Edit; The .Column(1) is missing because this is the name that is already inserted with the ComboBox.
There is some confusion caused by the wording of the question, I'll try to state back how I've interpreted and if I have it right it may lead you to an answer.
You have combo box called cmdName that is pre-populated with data from a table. The content of the combo box could look as below (you may have set column widths to zero to hide the data)
0001|Gary Evans|gary#email.com|01/Jan/1970
0002|J Rommers |JR#email.com |02/Jan/1970
When the user selects J Rommers Me.tbPersonalNumber is populated with Me.cmbName.Column(0) (0002) and Me.tbEmailadress is populated with Me.cmbName.Column(2) (JR#email.com) but Me.tbBirthday is not being populated with Me.cmbName.Column(3) (02/Jan/1970).
Assuming Me.tbBirthday is a text box with no code that might clear it out, I suspect the issue is within the combo box. Not being sure how your combo box is set up, I would suggets the following checks:-
In the combo box properties, does the Column Count equal 4?
In debug, with a breakpoint on Me.tbBirthday = Me.cmbName.Column(3), does it show you the date you are after?
If it is not there does the query that populates the combo box have it in?
Edit based on comments to help further: -
Change the query to SELECT Personel.PersonalNumber, Personel.Emailadress, Personel.Birthday, Personel.Name FROM Personel ORDER BY Personel.Name; this puts all the fields you want hidden at the front.
Change the column widths property of cmbName to 0,0,0, this first the ones you want hidden and leave the last one to fill the width of the combo box.
Ensure the column count property is still 4 as per the answer
Change your code as per below and Gustav's answer
Replacement code:-
Me.tbPersonalNumber = Me.cmbName.Column(0)
Me.tbEmailadress = Me.cmbName.Column(1)
Me.tbBirthday = DateValue(Me.cmbName.Column(2))
This accounts for the fields moving in the query and ensure the date shows as a date like you wanted.
Comboboxes (and Listboxes) always return a string, so convert that to a Date value:
Me!tbBirthday.Value = DateValue(Me!cmbName.Column(3))

Change mail recipients email address conditionally with vba

I have a button on my Access subform, that creates a new email based on a specific template depending on the value in a combo box. Most of these templates already have an existing To: email address.
This has to be replaced to the email address in the parent form of the subform in most cases (and that is being done with the current code) but if the value of the combo box is, say either 'x' or 'y' then it should insert another email in a second form where the main form and second form have linked fields.
The code that I have so far:
If Me![cboEmail].Value = "Your patient Initial" Then
oMail.To = Nz(DLookup("[strEmail]", "tblAddress", "[longDoctorID] = " & Me.Parent!longDoctorID))
ElseIf Me![cboEmail].Value = "Follow Up of your patient" Then
oMail.To = Nz(DLookup("[strEmail]", "tblAddress", "[longDoctorID] = " & Me.Parent!longDoctorID))
Else
oMail.To = Me.Parent![strEmailAddress].Value
End If
What does not work is the first two conditions, where the value is "Your patient initial" or if it is "Follow Up of your patient"
Any help will be appreciated!
[longDoctorID] = " & Me.Parent!longDoctorID)
Is this a string or an integer? If it's a string it needs to include two single. quotes
I don't understand the reason for Nz when you haven't specified the empty value? You are then trying to send an email To ""
Debug.print Me![cboEmail].Value
What does it give you?
I am sorry guys, you were right in asking the question of what does not work? Because it was working code! When I put in the Debug.Print line, it gave an empty value because there was no value in the original table and therefore was giving the default email address.
Sorry to have used up your time. That original code works. Thanks for looking into the issue.

Access Report Detail Format - Default Values and Referencing

This question is mainly for curiosity, but also, in the description, I had intended to highlight an infrequently documented behavior of Access.
Background
When creating an Access report, we can use the On Format method of the detail section to modify values or properties per-record. For example, assume we want to hide a field label when the value is empty:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If (IsNull(Me.SomeField) Or Me.SomeField = "") Then
Me.SomeFieldLabel.Visible = False
Else
Me.SomeFieldLabel.Visible = True
End If
End Sub
What I did not realize about this until today is that the assignment .Visible = False does not modify the instance of the label in the Detail section, but rather is modifying the definition of the label on the report.
This can be demonstrated with the following modifiction to the code:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If (IsNull(Me.SomeField) Or Me.SomeField = "") Then
Me.SomeFieldLabel.Visible = False
End If
End Sub
Assuming the label is initially visible (in form Designer), the event produces somewhat unexpected behavior: the label will remain visible until the first empty record; after that, it will remain hidden for all other records - I had originally expected that at each call of Detail_Format the controls where starting with their default definitions.
Question
Is there any way to reference the particular instance of a control within the Detail_Format event?
In this simple case of visible true/false, this is easily handled by just a simple if-then-else, but I can imagine more advanced scenarios where one might want to leave the default values in tact.
I don't believe so, in all of my experience, properties of report objects always apply universally across the report, not to specific instances of the abject (if the object is repeated).
For your example, I would use a text box instead of a label to label the field, and use something like =IIf(IsNull(Field1), "", "Label:") for the controlsource. That way it won't show anything if the field is null, yet still show the label text if there is a value.