I have a table with only a text column. Furthermore I have a form with a combobox. Now I want the combobox to requery on focus. So I add a method in vba:
Private Sub combobox1_GotFocus()
With combobox1
.RowSource = "SELECT text " + _
"FROM tblExample " + _
" ORDER BY text"
.Requery
End With
End Sub
When I now click on the combobox1, the dropdownlist does not open. Only when I click a second time on it, it opens. What is the problem here? If I put the two .-lines in comment, I can click the combobox just one time and an empty list shows up.
Notice: I simplified the problem. I have another formular in which I do the same and it works fine. I hope someone has an idea how to fix this.
When you requery a combo box, it temporarily loses focus. When you click on it again, it already has the focus, so the OnFocus event does not fire. You may want to requery the combo box before the user clicks on it.
Your OnFocus action is just fine but you could add something like this one as well.
Private Sub combobox1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Me.combobox1.SetFocus
Me.combobox1.Dropdown
End Sub
Related
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
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!
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.
I have a continuous form containing many records. Each record contains a combo box that has vba code filtering the viewable selections when it gains focus (it does this by changing the RowSource.
My problem is that if i click the combo box in record A and then click the combo box is record B, the display-filter for record B is applied to record A. I do not have this problem if I click another control in between clicking the combo boxes.
Why is the filter applied to A's combo box even though focus has (supposedly) been lost? And how do I prevent this display error from happening? The only solution I have now is to make an arbitrary control gain focus when focus for the combobox is lost, but obviously that can be annoying for the user...
Possibly relevant code (SuperintendentID is the name of my combobox):
Private Sub SuperintendentID_GotFocus()
'Filters SuperintendentID based on the task's division
Me!SuperintendentID.RowSource = "SELECT tblJoinDivisionSuperintendent.SuperintendentID, [FirstName] & "" "" & [LastName] AS Name, tblJoinDivisionSuperintendent.DivisionID FROM tblSuperintendent RIGHT JOIN (tblDivision RIGHT JOIN tblJoinDivisionSuperintendent ON tblDivision.ID = tblJoinDivisionSuperintendent.DivisionID) ON tblSuperintendent.ID = tblJoinDivisionSuperintendent.SuperintendentID WHERE tblJoinDivisionSuperintendent.DivisionID = " & Me!DivisionID & ";"
Me!SuperintendentID = Me!SuperintendentID.ItemData(0)
Me!SuperintendentID = Me!SuperintendentID.OldValue 'Prevents a new value from being assigned when the control first gains focus
End Sub
You might be firing both the GotFocus events for comboboxA and comboboxB, but the screen is only showing you the last value you changed.
You could add use a Form.Repaint method (where Form=your form object) as part of the GotFocus event to ensure that the screen was refreshed after changing the underlying datasource.
You could test this to make sure both events are firing by putting a debug.print statement in the event as well, such as debug.print "GotFocus ComboboxA"
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