I have been trying to find a code which can remove the selected items from a combobox for next records. My combobox´s name is FZ and my form saves records for tblPrägen. My combobox gets the data from a table that named as tblFixieren. I am getting syntax error from Access. To be honest, i am not sure whether this code works or not. I would appreciate it if someone could help me.
Private Sub Form_Open(Cancel As Integer)
With FZ
.RowSourceType = "Value List"
.RowSource = _
CurrentProject.Connection.Execute("SELECT DISTINCT tblFixieren.ZahnID_Fix
FROM tblFixieren WHERE tblFixieren.Zahntyp_Fix Like F* ").GetString(adClipString, -1, , ",")
End With
End Sub
Private Sub FZ_Click()
FZ.RemoveItem FZ.ListIndex
End Sub
Related
I have a form, with a listbox. User can choose record and add it to a list box using listbox. I want to create a VBA script to open a report and match all the values in listbox with corresponding records. So far i can do this with single one, but for many i don't know what is going on. Here is the code:
Private Sub btnSearchMany_Click()
Dim i As Long
With Me.List12
If .ListCount = 0 Then
MsgBox "Brak wybranych wpisow.Dodaj wpisy, wybierajac je z listy i wciskajac przycisk dodaj.", vbCritical
Exit Sub
End If
For i = 0 To .ListCount - 1
DoCmd.OpenReport "rptKKsy", acViewReport, , "[tblKKsy].[KKS]='" & .List12(i) & "'"
Next i
End With
End Sub
List12- lisbox that user can add records "later i will rename it"
rptKKsy- my report
tblKKsy- table that stores values
KKs - one of the values stored in my table
I always getting error: method or data member not found. I tried to replace:
With Me.List12
with
With Forms("frmSearch").Form.list12
But it just created another error.
Edit: As suggested, my list box is a value list, and user can put thing in it by using a combobox and clicking a button ( button have addItem command programed). After user add some things, i want to open a report for each value stored in listbox. I dont want user to select things in listbox, because we will be using everything in it so its not nedded. I tried to use Item.Data property as June7 linked to, but ill be honest: I dont get it. Here is the code:
Private Sub btnSearchMany_Click()
Dim i As Long
Dim lValue As String
With Me.List12
For i = 0 To .ListCount - 1
If .ListCount = 0 Then
MsgBox "Brak wybranych wpisow.Dodaj wpisy, wybierajac je z listy i wciskajac przycisk dodaj.", vbCritical
Exit Sub
End If
Next i
End With
If i = Len(lValue) > 2 Then
DoCmd.OpenReport "rptKKsy", acViewReport, , "[tblKKsy].[KKS]='" & .ItemData(lValue) & "'"
End If
End Sub
I want to save selected combobox value as global variable and select row of selected value.
I have an Excel file, where I want to make calculations based on inputs in sheet1 and data on sheet2.
Inputs are provided by combobox1 (list of names from column A in sheet 2), combobox2 (case yes/no) and combobox3 (values 1,2,3).
After I select value in combobox1 (for example: ABC which is value A7 from sheet2), I want to calculate from data in row 7 in sheet2:
B7 (sheet2) + C7 (sheet2) * combobox3 value + D7 (sheet2) * (combobox2(yes = 2 / no = 0).
Can anyone help me with that?
Public SelectedComboBox1 As String
Public SelectedComboBox2 As String
Public SelectedComboBox3 As integer
Public calculate2 As Integer
Private Sub ComboBox1_DropButtonClick()
Sheet1.ComboBox1.List = Sheet2.Range("A3:A46").Value
SelectedComboBox1 = Me.ComboBox1.Value
End Sub
Private Sub ComboBox2_DropButton()
With Me.ComboBox2
.AddItem "YES"
.AddItem "NO"
End With
SelectedComboBox2 = Me.ComboBox2.Value
End Sub
Private Sub ComboBox3_DropButton()
With Me.ComboBox3
.AddItem "1"
.AddItem "2"
.AddItem "3"
End With
SelectedComboBox3 = Me.ComboBox3.Value
End Sub
Public Sub Calculate2_click()
calculate2 = Sheet2.Range("B7") * Sheet2.Range("C7") * SelectedComboBox3+Sheet2.Range("D7")*??
Sheet1.Range("H10").Value = calculate2
End Sub
It seems like you added a ActiveX combobox. You might want to use a form-combobox instead for Excel Sheets. Nevertheless: in the Editor you can add varioous actions to events in the combobox. What you did is add a reaction to the button-down-click Event. There you told excel to fill the list and set the SelectedCombobox variable to the - as of yet undefined - value of the combobox.
What you might want is one sub to fill the list as you did and another sub reacting to the change event of the combobox. That sub will be called as soon as someone changes the value of the box:
Private Sub ComboBox1_DropButtonClick()
Sheet1.ComboBox1.List = Sheet2.Range("A3:A46").Value
End Sub
Private Sub ComboBox1_Change()
SelectedComboBox1 = Me.ComboBox1.Value
End Sub
This should get you a good start. There are plenty of resources out-there that teach you how to write effective macros in Excel.
If you just need a result that uses the value of these three comboboxes, you could also connect a simple form-comboboxes with respective cells and claculate the result out of those cells.
But if you still want to use vba, think about using just one button to trigger a sub and access the values immediately:
Private Sub Button1_click
cells("A1").value=Me.ComboBox1.Value * Me.ComboBox2.Value...
End Sub
My combobox called districtCombo is bound to a field called carDistrict. To help me save time updating the CarDistrict field, I want to be able auto fill the combobox as users type.
I thought of using a select statement in the districtCombo_Change event but I am not getting anything, not even any error message.
Is there a way of getting around this? Or any better suggestions?
Private Sub districtCombo_Change()
Dim Task As String
Task = "SELECT carDistrict FROM tblCars ORDER BY tblCars.districtCombo"
Me.districtCombo.RowSource = Task
End Sub()
My second try is bringing me closer i think. I have abandoned the Change event completely and I constructed a Form_Load event as follows:
Private Sub Form_Load()
Me.RecordSource = "tblCars"
districtCombo.ControlSource = "carDistrict"
districtCombo.RowSourceType = "Table/Query"
districtCombo.RowSource = "SELECT carDistrict FROM tblCars"
End Sub
the problem is, the combobox displays all the values including repeated values and empty fields. There a way of filtering the combobox items so that only one item made up of the same string is displayed?
I think I am getting closer but I have come upon another problem. If the value being typed is not on the list, I am not allowed to add it to the table. Is there a way? Updated code below:
Private Sub Form_Load()
Me.RecordSource = "tblCars"
districtCombo.ControlSource = "carDistrict"
districtCombo.RowSourceType = "Table/Query"
districtCombo.RowSource = "SELECT distinct carDistrict FROM tblCars WHERE
carDistrict Is Not null"
End Sub
Private Sub districtCombo_NotInList(NewData As String, Response As Integer)
Dim rs As DAO.Recordset
Set rs = CurrentDb.OpenRecordset("tblCars")
rs.AddNew
rs!carDistrict = NewData
rs.Update
End Sub
Private Sub districtCombo_AfterUpdate()
Me.Requery
End Sub
I am getting error on the rs.Update part.
And Finally, This is what I was looking for:
'Set the Limit to list property of the combobox to No
Private Sub Form_Load()
Me.RecordSource = "tblCars"
districtCombo.ControlSource = "carDistrict"
districtCombo.RowSourceType = "Table/Query"
districtCombo.RowSource = "SELECT distinct carDistrict FROM tblCars WHERE
carDistrict Is Not null"
End Sub
Private Sub districtCombo_AfterUpdate()
Me.Requery
End Sub
If the combobox has a valid Row Source containing all available options, items from the Row Source will automatically appear as the user types in the combobox.
If the list of possible values is not stored in a separate table (as you would when following the rules of database normalisation), then you can populate the combobox with all possible values by specifying the Row Source as something like:
select distinct cardistrict from tblcars where cardistrict is not null
You will also need to refresh the row source dataset following the addition of new values.
One way to achieve this is to invoke the Requery method of the combobox control within an event handler for the AfterUpdate event:
Private Sub districtCombo_AfterUpdate()
districtCombo.Requery
End Sub
Note that properties such as the form Record Source and combobox Control Source, Row Source, and Row Source Type do not need to be populated on the Load event of the form, but can be configured in the Form Design.
I am trying to set the values of the combo box to 0, 1, and 2 (hard coded values and not a cell reference) but it does not seem to work. I have tried setting the RowSourceType" to 1 and toValue List`, but I am getting compile errors every time. For example, the following code does not work:
Private Sub UserForm_Initialize()
Me.errorComboBox.RowSourceType = "Value List"
Me.errorComboBox.RowSource = "0;1;2"
End Sub
or
Private Sub UserForm_Initialize()
Me.errorComboBox.RowSourceType = 1
Me.errorComboBox.RowSource = "0;1;2"
End Sub
I am getting errors on the RowSourceType line for both. How can I do this?
To statically populate the list of a combo box in Excel, try something like this:
Me.errorComboBox.List = Array("0", "1", "2")
This SO question contains links and other examples to achieve what you want.
I have created a simple userform with a combobox populated with a range of dates (rngWeekList) but I am having serious headaches trying to get the list in the dropdown box to appear in "dd-mmm-yy" format. Here is my code:
Private Sub UserForm_Initialize()
' Populate the list with the date range
ComboBox1.List = Worksheets("Cover").Range("rngWeekList").Value
' Set the defulat selection (based off rngWeekIndex)
ComboBox1.ListIndex = Worksheets("Cover").Range("rngWeekIndex").Value - 1
' Format
ComboBox1 = Format(ComboBox1, "dd-mmm-yy")
End Sub
Private Sub ComboBox1_Change()
' Format
ComboBox1 = Format(ComboBox1, "dd-mmm-yy")
End Sub
This manages to format the selected item in the combobox correctly (e.g. "02-Jul-14") but when I open the dropdown list, all the list entries shown are formatted in the default "m/d/yyyy". Is there a way to change the formatting for the list entries? It is confusing for users who are used to seeing the day before the month.
Thanks in advance for your help, it is much appreciated.
Ed
I managed to fix it by looping through each item in the comboboax and formatting it (feel free to correct me if there is a more elegant way to do it!)
Private Sub UserForm_Initialize()
Dim i As Integer
' Populate the list with the date range
ComboBox1.List = Worksheets("Cover").Range("rngWeekList").Value
'Format all items
For i = 0 To ComboBox1.ListCount - 1
ComboBox1.List(i) = Format(DateValue(ComboBox1.List(i)), "dd-mmm-yy")
Next i
' Set the default selection (based off rngWeekIndex)
ComboBox1.ListIndex = Worksheets("Cover").Range("rngWeekIndex").Value - 1
End Sub
Private Sub ComboBox1_Change()
' Format the selection
ComboBox1 = Format(ComboBox1, "dd-mmm-yy")
End Sub
Sorry for posting, but I really thought I was stuck.
Thanks again,
Ed