Creating Message Boxes - ms-access

In Access 2010, I need to have a Message Box pop-up when the user selects specific text (Real Estate) in a combo-box (ProductType).
In short...if the [ProductType] entered by the user is any one of the multiple "Real Estate" options available, then show a Message Box that states "USER MUST COMPLETE RESPA TRACKING" and then direct them to that page (tab) to complete additional text fields.
I've attached the following code to the BEFORE UPDATE function of the combo box. But that only creates the message, it does not direct them to the tab/fields that need to be entered.
Private Sub ProductType2_BeforeUpdate(Cancel As Integer)
Dim strPrompt As String
strPrompt = "The Product Type selected is Real Estate. The RESPA Tracking tab MUST be completed at time of App Entry and validated during Underwriting."
If Not IsNull(Me.ProductType2.Value) Then
If Me.ProductType2.Value Like "Real Estate*" Then
Cancel = (MsgBox(strPrompt, vbOK) = vbNo)
End If
End If
End Sub

It seems you've solved the creating a MsgBox issue, and now your remaining challenge is to "direct them to the tab/fields that need to be entered".
Use the SetFocus method to place focus on a specific page in your tab control, or on a control within that page.
This command button example shifts focus to the first control on a page named Page1 ...
Private Sub cmdPage1_Click()
Me!Page1.SetFocus
End Sub
But my guess is you will prefer focus on a specific control within the page. My Page1 contains a text box named txtMemo_field. This version places the focus on that text box. Notice the page name is not mentioned in this version; SetFocus is called on the text box directly ...
Private Sub cmdPage1_Click()
Me!txtMemo_field.SetFocus
End Sub

Related

how to create a combo box in access which can be converted to text box to enable writing if the user needs to add a new option to the existings?

I was wondering how I can define a combo box in a form where the user can select an option from the existing list, and if he can't find the desired option, a "add a new option" can be accessed which converts the combo box to a text box for writing the new option?
You can put a button next to the combo, for an 'add option' which is triggered when the combo 'not in list' event is hit - and the button then would call a popup form - or in your suggestion, changes the visibility of an overlayed text box that allows for entry. I suggest a popup form, however, because then you can validate the input for length, content, etc - and then once the form or text box is filled in - exiting the text box, or submitting the form would save to your table for the drop down, refresh the combo box source query, and assign the value. I do this in VBA alot with popups.
Sorry my bad english :-(
You can directly use the "On Not in List" event. Look at the examples at
https://learn.microsoft.com/en-us/office/vba/api/Access.ComboBox.NotInList
This is the scenario: A combobox called cb and a textbox called tb. Visible property of tb is false. Combobox and Textbox have the same data source. When you type a value that is not in the combobox list, that value goes to the textbox; when you finished editing textbox, it is hidden again.
Private Sub cb_NotInList(NewData As String, Response As Integer)
' Suppress error message and undo changes.
Response = acDataErrContinue
Me!cb.Undo
' Prompt user to verify they wish to add new value.
If MsgBox("Value is not in list. Add it?", vbOKCancel) = vbOK Then
Me!tb.Visible = True
Me!tb.Value = NewData
Me!tb.SetFocus
End If
End Sub
Private Sub tb_AfterUpdate()
' Hide again texbox
Me!tb.Visible = False
' Update combobox items
Me!cb.Requery
End Sub

Tab control in VB

I'm working on a legacy VB6 application. I'm sure this probably relates to VB.NET so i will tag it, but please let me know if it's completely different(which I dont think it is) then I'll remove the tag to avoid confusion.
Here is my issue....
I have a Tab control with multiple tabs: 0 - 3. On TabStuff.Tab = 0, I have a few textboxes and comboboxes. The user uses keyboard TAB to move from Indexed controls. What happens is once they get to the last control which is a textbox called txtCity - and click keyboard TAB once more, it brings them to TabStuff.Tab=1.
My issue is I do VALIDATE on txtCity - I call a function that verifies that a couple of the fields aren't NULL and if one of the fields is in fact NULL then I show a MSgBox and try to setFocus on that control. But instead, when OK is clicked on msgbox, it goes to the next tab which is TabStuff.tab=1 which is not correct.
Here's some of my code...
Dim FirstName, City as String
flag=false
firstName = txtName.text
city = txtcity.text
if FirstName="" or isnull(FirstName) then
msgbox "Please enter Name"
tabstuff.tab=0
txtname.setfocus
exit sub
elseif city = "" or isnull(city) then
msgbox "Please enter city"
tabstuff.tab=0
txtcity.setfocus
exit sub
end if
flag=true
This code is in txtCITY_VALIDATE
So in case city was empty, it shows MsgBox, stays on Tab=0 and setfocus on that control, but instead it goes to the next tab=1 and sets focus on the first control of that tab.
EDIT:
in txtCITY_LostFocus
If Flag = False Then
TabStuff.Tab = 0
Exit Sub
End If
I added this but it still goes to tabstuff.tab=1 setting the focus on first control of the tab
EDIT 2:
In a new project i created txt1 and txt2 - i set TabIndex 0 and 1 respectively.
Private Sub Txt1_Validate(Cancel As Boolean)
If Txt1.Text = "" Then
MsgBox "no text in txt1"
Txt1.SetFocus
End If
End Sub
This is the code I use. I click TAB on txt1 without entering any text, so this gets executed, but after msgbox, the focus gets set on txt2
For some extremely weird reason - i seem to have been getting this discrepancy because I was doign it in the VALIDATE property. When i entered the same code in LostFOCUS it seems to work fine. Thanks everyone for your help with this!

Continous Form - Image control click event to recognise current record

I have a subform that looks like this that I'm wanting to use as a task list:
The task is marked as completed in the record using a Yes/No field called TaskStatus; this is shown as the checkbox control to the right in the above screenshot.
Rather than use this checkbox though, I'd like to do something a bit more fancy-pants and have the user click the image control called imgTaskIcon shown as the red flag on the left, which will both mark the TaskStatus field for the record appropriately and toggle its own image to that of either a red flag (not completed) or a green tick (completed).
At the moment, I have the control source of imgTaskIcon set to look at another (hidden) field in the record that is storing the path to the appropriate completed/not completed image.
I've then got this code running on imgTaskIcon click event, which essentially toggles TaskStatus from yes (-1) to no (0):
Private Sub imgTaskIcon_Click()
If _
Me.TaskStatus = -1 _
Then
Me.TaskStatus = 0
Else
Me.TaskStatus = -1
End If
toggleTaskStatusIcon
End Sub
And then toggleTaskStatusIcon, which sets the correct image path for the record:
Public Sub toggleTaskStatusIcon()
If _
Me.TaskStatus = -1 _
Then
Me.TaskStatusIcon = "P:\myPath\CompletedIcon.png"
Else
Me.TaskStatusIcon = "P:\myPath\NotCompletedIcon.png"
End If
End Sub
This works fine when toggling the first record:
However, clicking the image on the second record, just toggles the first record again:
By first clicking in to some other control in the record, I can then click the image control and the toggle works on the appropriate record. So it seems that an image control won't change the current record to its own record when it is clicked.
Is there a way to get an image control to be less ambivalent about where it lives?

Set focus to specific field in Access 2010

How do I set the focus on a particular field (in this case, the "Ref:" field which is located in the Form Header) when viewing a form in Form View?
You could use SetFocus in Form_Active
Private Sub Form_Activate()
yourField.SetFocus
End Sub
or you could change the order tab and put your field into first place of the list

MS Access de-select listbox after running macro/query

So I have a form (frmBookingForm) in Access, and a listbox (lstMyBookings) that displays the results of a query.
Below this, I have a button (cmdDeleteBooking) which runs a delete query using the lstMyBookings selection as input. The button then runs a macro that firstly checks whether a record in the listbox is selected, and if one is, runs the delete query and requeries the data, so the deleted record is no longer shown in the listbox. If not, an error message is displayed.
However, if I then click the button again, it again runs the delete query, even though there is apparently nothing selected in the list box.
Essentially, how can I 'clear' the listbox selection?
I'd prefer a solution that can be done in a macro format, as I have little to no understanding of VBA. However, if providing a VBA solution, I'd greatly appreciate a short explanation of it.
Thanks :)
Looks like this website has a nice little function to do it. Essentially, you need to test if it's a multiselect, and then do one of two things. I suppose if you know ahead of time that it is/isn't a multiselect, you wouldn't even need the "if" statement:
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
If the control's MultiSelect property is set to None, this code just sets List0 to Null. If the control's MultiSelect property is set to anything else, the code loops through all items that are currently selected, and sets the Selected property of that item to False. My example assumes your control is called List0.
EDIT
To use this code, use an event instead of a macro. Here's how you do this:
Right click on the button, select properties
In the Property Sheet window, click on the "Event" tab
Click inside of the "On Click" blank area, and click the dropdown arrow, then select "[Event Procedure]"
Click the ellipsis ("...") to go into the code editor.
In the code editor, your should already have your event for the button (let's assume the button is called Command1:
Private Sub Command1_Click()
End Sub
Add your code in between (assuming the listbox is called List0):
Private Sub Command1_Click()
If List0.MultiSelect = 0 Then
List0 = Null
Else
For Each varItem In List0.ItemsSelected
List0.Selected(varItem) = False
Next
End If
End Sub