I have a combo box called NameFilt. I wish the row source to be as follows:
SELECT DISTINCT VoucherListTbl.BuyerName
FROM VoucherListTbl
WHERE (((VoucherListTbl.BuyerName) Is Not Null)) OR (((VoucherListTbl.BuyerName)<>""));
i.e show all the unique BuyerNames from my table and dont include any blanks
The above SQL is generated in the query bulider by clicking on the 3 dots in combo box's row source in the property sheet, then selecting the BuyerName field and then entering Is Not Null Or <>" in the criteria. Clicking run in the query builder displays the exact result I expect.
On closing and saving the query builder and then clicking in the combo box on the form I get a different result - All the DISTINCT names are there, but there is a blank at the start of the list.
When I attempt to use this SQL in my VBA code I get another result. The code is:
Private Sub NameFilt_GotFocus()
Me.AllowEdits = True
Me.NameFilt.RowSource = "SELECT DISTINCT VoucherListTbl.BuyerName
FROM VoucherListTbl WHERE (((VoucherListTbl.BuyerName) Is Not Null))
OR (((VoucherListTbl.BuyerName)<>""));"
Me.NameFilt.Dropdown
End Sub
This results in the combo box's dropdown showing only one option - a blank! There are no names listed.
Moreover, If the WHERE clause is removed i.e. the code is:
Private Sub NameFilt_GotFocus()
Me.AllowEdits = True
Me.NameFilt.RowSource = "SELECT DISTINCT VoucherListTbl.BuyerName FROM VoucherListTbl;"
Me.NameFilt.Dropdown
End Sub
Then the DISTINCT names are shown, with a blank option at the top of the list which is what one would expect
Please could you help by explaining why the WHERE clause will not work for me when entered into the VBA code
Many thanks
If you use "" within "" it will break the string and that's the reason of non-working WHERE condition. Encode quotes with "" i.e. end of your original string should look like <>""""));" or replace "" with '' and try again.
Note, same query can be written as
SELECT DISTINCT BuyerName
FROM VoucherListTbl
WHERE IsNull(BuyerName,'')<>''
I am limited in that I have to stick with the old (messy) table design. On this one interface I am developing, there is a combo drop down that is a fixed values list. The options are Group1, Group2, and Group 3. Based on that, a dependent combo needs to be set to the correct row source and control source.
The form is locked, and the user can navigate the records (first, last, next previous, and an unbound combo to jump to a record). During those operations, I just want to refresh which list the dependent combo should pull from, and what value it should bind to, to show the correct value.
When the user clicks to make a "new" record, I won't show the dependent drop down (or make it enabled. haven't decided yet) until they choose a value in the first combo, telling us which group it is.
In order to control this, I created a function for use on this form. This is that function:
Private Function MyGroup()
'Mybitval is a table value that is a bit data type. It means group1
If Me.Mybitval = True Then
Me.cboGroupType.Value = "Group1"
ElseIf Me.Mybitval = False And Nz(Me.MyID1, 0) <> 0 Then
Me.cboGroupType.Value = "Group2"
ElseIf Me.Mybitval = False And Nz(Me.MyID1, 0) = 0 Then
Me.cboGroupType.Value = "Group3"
End If
Select Case Me.cboGroupType
Case "Group1"
Me.cboGroupName.RowSource = "SELECT Group1.ID, Group1.G1Name FROM Group1 ORDER BY Group1.G1Name;"
Me.cboGroupName.ControlSource = me.FKID1
Case "Group2"
Me.cboGroupName.RowSource = "SELECT Group2.ID, Group2.G2Name FROM Group2 ORDER BY Group2.G2Name;"
Me.cboGroupName.ControlSource = Me.FKID2
Case "Group3"
Me.cboGroupName.RowSource = "SELECT Group3.G3ID, Group3.G3Name FROM Group3 ORDER BY Group3.G3Name;"
Me.cboGroupName.ControlSource = me.FKID1
End Select
Debug.Print Me.cboGroupName.RowSource
Debug.Print Me.cboGroupName.ControlSource
Debug.Print Me.cboGroupName.Value
End Function
The idea is that I call this function on load, after I go to the first record, and then on any navigation button click or after update of the go to combo.
The problem is, that the cbogroupname never shows the value it has in the control source. When I try to load the form, my debug rowsource shows the correct sql, and the control source shows the value of the correct table field for the current record it's on, but 2 things don't seem to work. If I don't do the 3rd debug on the value, the form loads fine, but the combo doesn't show the right value, even though it's bound to a table column that has a value. If do debug the value, I get a run-time error '2424': "The expression you entered has a field, control, or property name that [my application] can't find."
When I hit debug, it goes to the last line of the function:
Debug.Print Me.cboGroupName.Value
When I go to save a new record, I run through validation, and having this 1 combo, instead of 3 different ones with alternating the visibility, seems way easier. Is there a reason this isn't working the way I am wanting it to?
Thanks!
And just like that, I figured it out. Of course it takes me all day, and posting this question to get it.
Here is the function, with the right syntax:
Private Function MyGroup()
If Me.Mybitval = True Then
Me.cboGroupType.Value = "Group1"
ElseIf Me.Mybitval = False And Nz(Me.MyID1, 0) <> 0 Then
Me.cboGroupType.Value = "Group2"
ElseIf Me.Mybitval = False And Nz(Me.MyID1, 0) = 0 Then
Me.cboGroupType.Value = "Group3"
End If
Select Case Me.cboGroupType
Case "Group1"
Me.cboGroupName.RowSource = "SELECT Group1.ID, Group1.G1Name FROM Group1 ORDER BY Group1.G1Name;"
Me.cboGroupName.ControlSource = "FKID1"
Case "Group2"
Me.cboGroupName.RowSource = "SELECT Group2.ID, Group2.G2Name FROM Group2 ORDER BY Group2.G2Name;"
Me.cboGroupName.ControlSource = "FKID2"
Case "Group3"
Me.cboGroupName.RowSource = "SELECT Group3.G3ID, Group3.G3Name FROM Group3 ORDER BY Group3.G3Name;"
Me.cboGroupName.ControlSource = "FKID1"
End Select
Debug.Print Me.cboGroupName.RowSource
Debug.Print Me.cboGroupName.ControlSource
Debug.Print Me.cboGroupName.Value
End Function
Provided your form is bound to a table, such as mine is, the control source needs to = "fieldname" . It needs to be the field name in double quotes. Not me.fieldname as that ends up making the controlsource the value of that field name. It also can't be [table1]![field1] I tried that and that doesn't work either.
The above works beautifully. Now to test the rest of the form!
I'm trying to make a form checkbox update a date in another table.
Private Sub Delivered_AfterUpdate()
If Delivered = -1 Then
[tool implentation].[date] = Now()
End Sub
I'm getting an error on the [tool implentation].[date] = Now() line.
I would like if any time the checkbox is clicked (check and un-check) the date is updated.
I'm assuming your table is also the form's recordset here. If it is not you will need something to determine which record in the table you want to edit.
Private Sub Delivered_AfterUpdate()
If Me.Delivered = True Then
With Me.Recordset
.Edit
![ImplementationDate] = Now()
.Update
End With
End If
End Sub
You can leave Me.Delivered = -1 if you want but I find True/False to be easier to read.
I also recommend not naming something Date, I'm not sure if it actually matters as a column name, but it is both a builtin function and data type. So I changed it.
You should also indent your code, it will be a nightmare as you get more lines.
I have a pulldown text field with 15 options - Package 1, Package 2, through to Package 15.
Once a user selects a pulldown, the on change procedure is such that it populates the the fields in the form with the cooresponding data pulled in the select statement as the source of the pull down.
It works great until the user selects Package 10 or greater. the result is blank fields. Not exactly sure why and how to fix this.
Private Sub ColourPackage_Change()
Me.Brick.Value = Me.ColourPackage.Column(1)
Me.Stone.Value = Me.ColourPackage.Column(2)
Me.Shingles.Value = Me.ColourPackage.Column(3)
Me.Windows.Value = Me.ColourPackage.Column(4)
Me.GarageDoors.Value = Me.ColourPackage.Column(5)
Me.BoardBatton.Value = Me.ColourPackage.Column(6)
Me.Alluminum.Value = Me.ColourPackage.Column(7)
Me.Stucco.Value = Me.ColourPackage.Column(8)
Me.Shakes.Value = Me.ColourPackage.Column(9)
Me.Railing.Value = Me.ColourPackage.Column(10)
End Sub
Any help is greatly appreciated.
Use the AfterUpdate event:
Private Sub ColourPackage_AfterUpdate()
I am applying some SQL query into a spreadsheet, and have trouble to minimize number of worksheets/connections.
i have 3 scenarios, for each of them, I use same query
select *
from ***scenario_table***
where mycolumn > 100
Now I have to do this three times for different "scenario_table", and I really wanna to use a cell as reference (let's say cell $A$1)
I wanna sth like this
select *
from
case when [$A$1] = 1 ***scenario1_table***
case when [$A$1] = 2 ***scenario2_table***
case when [$A$1] = 3 ***scenario3_table***
where mycolumn > 100
my I know whether there is any way to work it out?
Thanks
This solution uses vba.
Assuming your cell with content is in Sheet1!A1 and there is only one conecction in your workbook, insert following code in Sheet1:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A1")) Then
Select Case Me.Range("A1").Value
Case 1: SetTable ("Scenary1_Table")
Case 2: SetTable ("Scenary2_Table")
Case 3: SetTable ("Scenary3_Table")
End Select
End If
End Sub
Sub SetTable(ByVal TableName As String)
With ThisWorkbook.Connections(1)
.ODBCConnection.CommandText = "SELECT * FROM " & TableName
.Refresh
End With
End Sub