I need to show denominations based on selection in combo box in my access form.
The tricky thing here is that I need to show immediately after selecting in the combo box (without saving it). This one is working just after I save my selection.
If cmb_Main_Impact.Value = "Productivity" Then
Me.txt_Units = "minutes"
End If
If cmb_Main_Impact = "Quality" Then
Me.txt_Units = "number of errors"
End If
That code should work fine. You want to put it in the combobox on change event.
Also added an elseif so there isnt multiple if and end ifs. Put that code in in the userform that has the combo box.
So your code will look like.
Private Sub cmb_Main_Impact_Change()
If cmb_Main_Impact.Value = "Productivity" Then
Me.txt_Units = "minutes"
elseIf cmb_Main_Impact = "Quality" Then
Me.txt_Units = "number of errors"
End If
End Sub
Related
Here's the short version: Need help figuring out why a combobox on a form is misbehaving. The combobox loads with a rowsource query, and the form load event procedure writes a default text in the combobox as a sort of user input prompt that isn't in the query list but seems to show up just fine when the form loads. When I change the rowsource query through code in another event procedure I can no longer write the same default text into the combobox. If this is because the default value isn't part of the new rowsource query list (which is the same query but with a WHERE clause to filter some of the options), then why can I do this before changing the rowsource even though it also doesn't match any records among the combobox's default rowsource?
Here's the long version:
I'm working on a construction cost database and modeling tool, and the form in question is an intake form for new conceptual projects with a few textbox and combobox fields for user input.
For aesthetic reasons I want to show default values for each input field in grey as a sort of user instruction prompt, and then change these to black as the fields are completed. There's code in the Form_Load() event procedure which writes the default value in all the controls, and also changes the text color to grey. The GotFocus() event procedure for control then changes the color to black and writes the value to vbNullString so the user doesn't have to delete the previously written default value. Then after the user moves on to another field the LostFocus() event procedure tests if a change has been made, either by typing into the text box in question or selecting from the list if it's a combo box, and will reset the default value as well as change color back to grey if no change has been made (or leave as is in black color if a change was made). This much of the code seems to be working fine.
Here's the issue:
One of the comboboxes asks for 'Space Type' and another one for 'Space Subtype'. When the user selects a particular space type for the project, I want to filter the space subtype list options to show only those related to that space type. I've done this by rewriting the cmbSpaceSubtype.RowSource query within the cmbSpaceType_Change() event procedure, and this also seems to be working fine. The problem is after the row source query is changed through this event procedure, it seems I can no longer rewrite the cmbSpaceSubtype text to the default value that I'd like to show until the user makes a selection for that field. I would think this is obviously because the default value I'm using is not an option on the query list that the row source now points to. But if that's the case then why can I use an identical line of code to write in the same default value through the Form_Load() event procedure, even though the default value also not an option on the row source for that combobox when the form is loading?
Here's a link to the Access file: [https://app.e-builder.net/public/publicLanding.aspx?QS=b3a5c010645942a9881b361dae7433ae][1]
Here's an excerpt of the vba code:
Option Compare Database
Private Const sListDefault As String = "Select from list"
Private Sub Form_Load()
With Me
Let .cmbSpaceSubtype = sListDefault
Let .cmbSpaceType = sListDefault
End With
End Sub
Private Sub cmbSpaceSubtype_GotFocus()
With Me.cmbSpaceSubtype
If .Text = sListDefault Then
Let .Value = vbNullString
Let .ForeColor = vbBlack
End If
End With
End Sub
Private Sub cmbSpaceSubtype_LostFocus()
With Me.cmbSpaceSubtype
If .Text = vbNullString Or IsNull(Me.cmbSpaceSubtype) Then
Let Me.cmbSpaceSubtype = sListDefault
Let .ForeColor = vbTextLight
End If
End With
End Sub
Private Sub cmbSpaceType_Change()
With Me.cmbSpaceSubtype
If Me.cmbSpaceType.Text = sListDefault Then
Let .RowSource = "SELECT [ID], [Space Subtype] FROM tblSpaceSubtypes ORDER BY [Space Subtype];"
Else
Let .RowSource = "SELECT [ID], [Space Subtype] " _
& "FROM tblSpaceSubtypes WHERE [Space Type]=" & Me.cmbSpaceType.Value & " ORDER BY [Space Subtype];"
End If
Let Me.cmbSpaceSubtype = sListDefault
Call .SetFocus
Let .ForeColor = vbTextLight
End With
End Sub
Private Sub cmbSpaceType_GotFocus()
With Me.cmbSpaceType
If .Text = sListDefault Then
Let .Value = vbNullString
Let .ForeColor = vbBlack
End If
End With
End Sub
Private Sub cmbSpaceType_LostFocus()
With Me.cmbSpaceType
If .Text = vbNullString Or IsNull(Me.cmbSpaceType) Then
Let .Value = sListDefault
Let .ForeColor = vbTextLight
End If
End With
End Sub
Finally figured it out ... resetting the combo box to a default value after changing the rowsource did execute properly but I wasn't seeing the default value on the form because it's a multicolumn row source and the bound column was width 0, so the value was hidden.
I have a combo box with a list of customers which I am filtering as the user types.
When the form loads there is a preset value in the combo box, however when I backspace the combo box doesn't update the value (it keeps the original customer). The value will only become null once I click away from the combo box. Then I click back and it starts filtering as I type.
I tried setting the focus to another control and then coming back to the combo box on the AfterUpdate events but the code doesn't change the focus
Private Sub cboCustCode_AfterUpdate()
Me.txtCustName = DLookup("[CUST_NAME]", "[PLACE_HOLDER_CUST_LIST]", "[CUST_CODE] = '" & Me.cboCustCode & "'")
Me.txtCustName.SetFocus
Me.cboCustCode.SetFocus
End Sub
I want to be able to backspace in the combo box and then start typing and see the filtered results
I fixed it by checking if the combo box text length is 0 and then setting the combo box value = null
Private Sub cboCustCode_Change()
Me.txtCustName = DLookup("[CUST_NAME]", "[PLACE_HOLDER_CUST_LIST]", "[CUST_CODE] = '" & Me.cboCustCode & "'")
If Len(Me.cboCustCode.Text) = 0 Then
Me.cboCustCode = Null
End If
End Sub
I've created an ActiveX dropdown list and each option is linked to a bookmark for the text. Under the ActiveX controls there are the bookmarks (R1 andR2), hidden.
When I hit the btnselect button, all the other bookmarks, except the selected one, get deleted and the selected one becomes visible.
In the bookmark R2
I have a MacroButton for showing/hiding another text (CollapseMentiuniReclamant). When clicking the button it runs either Expand1 sub or Collapse1 sub, but the bookmark CollapseMentiuniReclamant doesn't show up.
I've simplified the document and codes as much as possible. Link to the document - https://wetransfer.com/downloads/1caea3c5d3b05e226e8b8f6a29760ad220190522071742/15db59.
The vba code is:
Private Sub btnselect_Click()
If ComboBox1.Value = "1" Then
Bookmarks("R1").Range.Font.Hidden = False
Bookmarks("R2").Range.Font.Hidden = False
Bookmarks("R2").Range.Delete
End If
If ComboBox1.Value = "2" Then
Bookmarks("R1").Range.Font.Hidden = False
Bookmarks("R1").Range.Delete
Bookmarks("R2").Range.Font.Hidden = False
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = True
End If
End Sub
Sub Expand1()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Collapse1").Insert _
Where:=Selection.Range
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = False
End Sub
Sub Collapse1()
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Expand1").Insert _
Where:=Selection.Range
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = True
End Sub
Update: I've simplified the last part of code and the problem still persists:
Sub Expand1()
Bookmarks("CollapseMentiuniReclamant").Range.Font.Hidden = False
End Sub
I've even removed the button entirely and ran the macro from View Macros Tab and it's not working.
Why doesn't CollapseMentiuniReclamant show up?
It's not showing up because what you're trying to hide/unhide isn't inside the bookmarked range. In any event, you should be inserting/deleting the content, not simply toggling it's hidden property. Making something hidden is no guarantee it won't be seen or printed (even if not seen), as those settings depend on how the end user has Word configured.
C, Thank you for your input and encouragement! I have changed my form and script slightly, I am afraid I kept the if then statement as I am comfortable with the formatting. The script now works when the 'On Open'event runs.
Private Sub Form_Open(Cancel As Integer)
Me.ChkAlbumNotes.SetFocus
If Me.ChkAlbumNotes.Value = False Then
Me.lblAlbumNotes.Visible = False
Me.txtAlbumNotes.Visible = False
Me.btnAlbumNotes.Visible = True
Else
Me.lblAlbumNotes.Visible = True
Me.txtAlbumNotes.Visible = True
Me.btnAlbumNotes.Visible = False
End If
Me.TrackName.SetFocus
If Me.TrackName = " " Then
Me.btnAddRecord.SetFocus
Else
Me.btnNextRecord.SetFocus
End If
End Sub
This is fine when the form opens for the first time but I have a set of navigation buttons that are installed by the application as Macros. I cannot add my script to the On_Click event when the button is clicked, as On_Click is linked to the Macro. Is there a way to incorporate the script from the On_Load process to the pre-defined macro? Or can you suggest a neater way to achieve my requirements which are;
When the form opens,a check is made for the existence of a false value in the checkbox
if the check box is set to false, then the Notes Text Box and label are hidden and the notes button is visible.
If the check box has a true value, then the Notes text box and label are made visible and the button is hidden.
On completion of the test I check the field Track Name
if this is empty, I assume I am at the last record and give the Add New Record button the focus
If Track Name is not empty, then focus is set to Next Record button
when this button is clicked, the next record page opens and the process starts again.
Many Thanks
Mike
You should use the Form_Current event instead of Form_Open . This fires on starting the form (2 times) and everytime you move to another record.
Private Sub Form_Current()
Me.lblAlbumNotes.Visible = Me.ChkAlbumNotes.Value
Me.txtAlbumNotes.Visible = Me.ChkAlbumNotes.Value
Me.btnAlbumNotes.Visible = Not Me.ChkAlbumNotes.Value
If Me.TrackName = "" Then ' I suggest If Me.TrackName = " " being a typo and you want to check if empty ( that's why you should use vbNullString instead of "")
Me.btnAddRecord.SetFocus
Else
Me.btnNextRecord.SetFocus
End If
End Sub
I have an access form that needs filling in daily by various people.
It's to document changes to a website and I currently have a combobox box set up for the various sections to state whether they are AMENDS, REVERTS or NO CHANGE.
I have set conditional formatting to then highlight these sections but am also trying to get it to work so that if the user chooses "NO CHANGE" then the data for that field copies over from the previous record.
I have set this up in the AfterUpdate code for the combobox, but nothing is happening, not even an error... can anyone help?
Private Sub COMBOBOX1_AfterUpdate()
If Me.COMBOBOX1 = 3 Then
Me.[FIELD_TO_CHANGE] = DLookup("[FIELD_TO_CHANGE]", "tb_TABLE", "[ID]=Forms![form_FORM]![ID]-1")
End If
End Sub
(Where 3 is the value of NO CHANGE in the combobox, and FIELD_TO_CHANGE, tb_TABLE and form_FORM being the names of the various elements)
Thanks!
First you should define your control COMBOBOX1, enter in Properties Window, define
COMBOBOX1.AfterUpdate = "[Event Procedure]"
then your Private Sub COMBOBOX1_AfterUpdate() will be taken into account. Error may occur and popup to you.
Then change the event handler like this to start:
Private Sub COMBOBOX1_AfterUpdate()
If Me.COMBOBOX1 = 3 Then
Me.[FIELD_TO_CHANGE] = DLookup("[FIELD_TO_CHANGE]", "tb_TABLE", "[ID]=" & (Me.[ID] - 1))
End If
End Sub
There will be many errors to correct before your form works...