Change mail recipients email address conditionally with vba - ms-access

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.

Related

Change value of single textbox in a recordset

First off I apologize for not knowing a lot about Microsoft Access forms. I have had very little involvement with them and this is only to support a legacy product that will soon be replaced.
I have a Microsoft Access form that has a detail section such as the image below (shown in Design View).
I believe the first 6 textboxes are bound to a data set, the last one is bound to nothing (I added it for what I'm trying to do). When I view the form in Form View I see many results (which come from the bound table). The only textbox that does not have info in it is the last one to the right (Unbound).
My goal is to just simply change the value and back color of the following textbox in red.
I want to edit the contents (change "jk" to "Error") and I want to change the background of that one textbox to RED (vbRed). The problem is when I try to change the backcolor it changes all the textboxes called "Errors" (that entire column).
ErrorHandler:
Forms![MyForm1]![Errors].BackColor = vbRed
If I want to get the value of the currently selected cell I can just do the following and it works fine...
MsgBox "Debug: " & Forms![MyForm1]![Code] 'The first column
MsgBox "Debug: " & Forms![MyForm1]![Name] 'The second column
MsgBox "Debug: " & Forms![MyForm1]![Number] 'The third column
MsgBox "Debug: " & Forms![MyForm1]![Errors] 'The Unbound (last column)
To edit and save individual records, the textbox must be bound to a field of the table.
So, add a field of Short Text to the table to hold your error message.

Referencing the value of a hyperlinked text box

So I'm having some difficulties with this code. I know it's obnoxiously wordy, but every attempt I made to turn some of these form references into variables to save space ended with me having even less functionality than before.
Basically what I've done so far is create a navigation form with several tabs, one to create a ticket, one to resolve/edit a ticket, and one to search the tickets. The search tab is basically a continuous form that updates based on the search criteria I enter. My goal is that when I click on the ticketID for each record, it will take me to the selected record on the Resolve/Edit Ticket page (on that page I have a combo box [called cboGoToRecord] where you can select the record you want).
I have a hyperlink in place that takes the user to the Resolve/Edit page and code that works ONLY when the line I've denoted with four asterisks (for clarity) is replaced with
rst.FindFirst "ticketID =" & [some number].
When I do that, the results are as expected. If I leave it as it is below, every record looks up the first record (A Debug.print check shows that the value of this field is apparently always 1...) So I guess what I need to figure out is how do I access the ticketID hyperlink's value so that I can put it on that line and make my code function effectively? I apologize if this is overly detailed but figured too much was better than not enough.
Private Sub ticketID_Click()
'Takes user from Search Tickets to Resolve/Edit Issues tab
DoCmd.BrowseTo acBrowseToForm, "frmResolveIssues", "frmBrowseTickets.NavigationSubform"
On Error Resume Next
Dim rst As Object
Set rst = Forms!frmBrowseTickets!NavigationSubform.Form.RecordsetClone
[Forms]![frmBrowseTickets]![NavigationSubform].Form![cboGoToRecord].Value = [Forms]![frmBrowseTickets]![NavigationSubform].Form![ticketID].Value
****rst.FindFirst "ticketID =" & [Forms]![frmBrowseTickets]![NavigationSubform].Form![cboGoToRecord].Value
Forms!frmBrowseTickets!NavigationSubform.Form.Bookmark = rst.Bookmark
Debug.Print [Forms]![frmBrowseTickets]![NavigationSubform].Form![ticketID].Value
End Sub
Edit:
After altering my form to add a separate hyperlink and referencing the static ticketID, I have concluded that everything I thought was true was not. Finding the value of a hyperlink was NOT the problem. The problem is that my ticketID value truly does insist on being one, and I have no clue how to fix that.
When this works:
Debug.Print [Forms]![frmBrowseTickets]![NavigationSubform].Form![ticketID].Value
then also check out:
Debug.Print [Forms]![frmBrowseTickets]![NavigationSubform].Form![cboGoToRecord].Value
As June7, I never use the Navigation form. It complicates everything too much.

Opening form from combo box in another form

I have a form called DisplayForm. In that form is a combo box drop down that is at the top of column on the form where a label would usually go. I want to select an item from that drop down menu and use that bit of data to open another form. I have copied an example from the web, changed the names and can't get it to work. Here is the code;
If Not Me.NewRecord Then
DoCmd.OpenForm "AreaForm", _
WhereCondition:="LArea=" & Me.AreaCBDrop
End If
Area is the name of the field in the query that is the recordsource for the form, but when I run it, it opens a msgbox that wants me to enter a peramater value. I also don't understand what the IF is about. I have tried this with and without the if but get the same result. Me.AreaCBDrop has the correct value in it, but the where does not work.
Thanks
Thanks
Your WHERE condition is expecting a text parameter, but you are not supplying the expected format, so it is asking for one.
Surround your Me.AreaCBDrop with single quotes, like this:
If Not Me.NewRecord Then
DoCmd.OpenForm "AreaForm", _
WhereCondition:="LArea='" & Me.AreaCBDrop & "'"
End If

Access Dynamic IIF reference on form

I cant seem to figure out how to reference a combo box value on a form in another textboxs sum iif function. The sample of what I am trying to do is below, with the reference to "TestCombo" the value I want to change based on who is selected. I tried using CHR(34) quotes, and I cant seem to figure it out.
SUM(iif([SaleStatus]= 'Pending' And [SalesAdvisor] = [TestCombo],1,0))
This same function works if I do the following
SUM(iif[SaleStatus]= 'Pending' And [SalesAdvisor] = 'TestAdvisor',1,0))
That led me to believe I needed quotes, which I expected, but that doesn't work either, such as the example below.
SUM(iif"[SalesStauts]= 'Pending' And [SalesAdvisor] = " & chr(39) & [TestCombo] & chr(39) & "",1,0))
Can somebody help me use the combo box as a dynamic reference to the sum iif?
Are you sure that your combo is equal to 'TestAdvisor'? It is quite common to have a numeric bound column and a text display. If you have more than one column in the combo, you will need to refer to the column property to get the text column. If you include the RecordSource for the combo in your question, it will be easy to see.

Cannot reference to a form control which is out of focus

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.