Access- How does one create a caption that disappears upon click? - ms-access

So I'm making a form and I want it so under the text it says "Optional" and upon typing the word "optional" goes away. Sort of like how the search bar above says "Search..." and upon typing it removes the word "Search...".
Please and Thank you, the learning curve is steep for me so any help will be useful.

In the Format-property of your textbox (1st property on the 1st tab) you can put #;"Optional" and you probably want to set the foregroundcolor to some shade of grey.
Then add a keyup-function to reset the text to black when the user enters text:
Private Sub myTextbox_KeyUp(KeyCode As Integer, Shift As Integer)
myTextbox.ForeColor = vbBlack
End Sub

Let's say your text box (we'll call it Text2) is labeled "Phone Number (Optional):" and the label name is Label1. You could use the Change event as Wayne suggested to change the value of Label1 to just "Phone Number:":
Private Sub Text2_Change()
Label1.Caption = "Phone Number:"
End Sub

Related

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!

Access 2007 textbox is seen as empty while it is not

I was searching for my question for hours and I have found some ideas but it still doesn't do what I want.
I have a form "AddWorker" with texboxes "Name", "Surname" and so on.
At the end of this form I decided to use a button to handle addition to database by VBA code.
When I put some data into texboxes I can refer to them by textbox.text property, but only if I am focused on this texbox. In other cases I can use textbox.value property.
In my case when I put all data into 3 textboxes I clik button "Add worker" to add person to database, but last textbox (for example 3) is seen as empty because it did't see text I have put into textbox. I need to clik into another textbox (for example 2 or 1) to create "some event" and then it update textbox 3 and all data can be read in vBA.
What can I do to see all texboxes filled in VBA when i click "Add worker" button.
I have found some example but it didn't help me a lot. I still see empty textbox while clicking "Add worker" button.
Textbox null problem
First of all, make sure the form is unbound. It should not have a recordsource associated with it.
Then, try adding a few messageboxes in the code behind the Add Worker button. Something like:
Sub Add_Worker_Click()
msgbox "Text1: " & Me.TextBox1.Value
msgbox "Text2: " & Me.TextBox2.Value
msgbox "Text3: " & Me.TextBox3.Value
/* Comment out all of your code for now */
End Sub
Give that a shot and see what is in the textboxes. Then replace ".Value" with ".Text" and see what you get.

Creating Message Boxes

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

How to make Access select the entire text in a box instead of all records when using ctrl+a?

In Access, is there a way to set what is selected when a user hits ctrl+a? I have a text box for users to input data, but if they hit ctrl+a, it selects all of the records instead of the entire text of a field. Is there a way to get around this? I am worried about someone accidentally deleting a bunch of records instead of the text of a field.
I'm a beginner user, so please forgive me if I am asking a silly question. I tried to search the forum, but haven't found a good answer.
Working with a textbox and the Keydown event, you can say:
Private Sub Content_KeyDown(KeyCode As Integer, Shift As Integer)
' Debug.Print KeyCode, Shift
If KeyCode = vbKeyA And Shift = acCtrlMask Then
MsgBox "Please use F2"
KeyCode = 0
Shift = 0
End If
End Sub
The textbox is called Content in this case.
You may like to get more complicated and look into KeyPreview.

I would like a new text box comes out when someone clicks on others

I have a form in access that dropdown box. Now what I want is when someone choose others from the dropdown box, a new text box shows up to allow the user to type. Anyone help please thank y ou
Assuming your DropDown is called Combo1 and on the same form you have a textbox called Text1 and the value of "Others" is also "others". Then you just need to code the Combo1's Change event like so
Private Sub Combo1_Change()
Me.Text1.Visible = (Me.Combo1.Value = "others")
End Sub