Access Cascading Combobox clearing after change - vba

I have some code to create a cascading Combobox in Access, which works correctly; the combobox filters to the list that I need. However, when I change the combobox it clears automatically, so I cannot use it.
Private Sub cobVehType_Change()
If CycleType(DLookup("VehicleType", "tblVehicleType", "VehicleTypeID =" & Me.cobVehType)) = "Miles" Then
Me.cobMainType.RowSource = "SELECT tblMainType.MainCycle FROM tblMainType WHERE (tblMainType.MainType)='Miles' And (tblMainType.SimilarTo)=False"
Else
Me.cobMainType.RowSource = "SELECT tblMainType.MainCycle FROM tblMainType WHERE (tblMainType.MainType)='Hours' And (tblMainType.SimilarTo)=False"
End If
End Sub
What else should I check?

You mean the combobox list does not have any items? Try refreshing the combobox:
Me.cobMainType.Requery
Should not need the DLookup as cobVehType should have all the info needed to provide the vehicle type.

Related

Access 2013 - Save Value from Textbox and display again when form is opened after close

I want to save a value from textbox in a string for example and display it again when the form get's openend.
I have two textboxes PriceRangeOne and PriceRangeTwo .. The user enter here for example 20 and 40
The problem i have is that when the user switches between Form and Report the values in this textboxes are beeing deleted. How can i save them?
I tried adding a sourcecontrol to the fields but had name errors eventhough i used different names.
I tried adding this to on change and retrieve it in an onload
Dim eingabe As String = textBox1.Text or .Value
Still didn't worked. Does anyone know a way to do this?
Typically, the most efficient and reliable way to do this is to have some form auto-open when the database is opened. It could be a dashboard, or just some form with nothing else on it. Whatever you use, launch it when the database opens and then minimize it. Now you have a form that's always open, as long as the application is open. Add a couple of textboxes to this form/dashboard.
When you close your form referenced in this question, write the values of PriceRangeOne and PriceRangeTwo to the textboxes on the form I described above. Then, when you open a new form or report, you can reference the values in those textboxes. Since the form is always open, you can reference these values at any time from any form or report until you close your database.
Solved it with variables.
I declared global variables in my standart module
For example
Public PriceOne As Double
Public PriceTwo As Double
Than i did this in my form in Close() and Open():
Private Sub Form_Close()
PriceOne = Me.Field
PriceTwo = Me.FieldTwo
End Sub
Private Sub Form_Open(Cancel As Integer)
Me.Field = PriceOne
Me.FieldTwo = PriceTwo
End Sub
Works perfect!
Courtesy of How to save the last value of a textbox. | Access World Forums:
Private Sub Form_Close()
'Set the default value for textbox
DoCmd.SetWarnings False
DoCmd.RunSQL "UPDATE table SET table.field = [Forms]![FormName]![Textbox] " & vbCrLf & _
"WHERE (((table.ID)=1));"
DoCmd.SetWarnings True
End Sub
Private Sub Form_Load()
'Load the default value for textbox
Me.Textbox.Value = DLookup("[field]", "[table]", "[ID]=1")
End Sub

Problems when calling a public sub

I'm facing a deadend When trying to call this sub :
Public Sub backblue(ByVal frm As Form, ByVal boxname As String)
For i = 1 To 3
CType(frm.Controls(boxname & i.ToString()), TextBox).BackColor = Color.LightBlue
Next
End Sub
with button click event :
Private Sub Button1_click and bla bla....
backblue(Me, "txb1_")
End Sub
Can anybody show me a suggestion to fix the code.
It throws "Object Referrence not set to an instance bla bla" error
For information the textbox names are :
txb1_1 , txb1_2 , txb1_3
(these are some of the many textboxes in the form that i want its bakcolor changed)
and these three textboxes are already created through designer, not from execution.
i did check the textboxes names and there's nothing wrong.
the form class is also public.
if they are the only textboxs on said form you can just loop through
For Each box as Textbox In frm.Controls
box.BackColor = Color.LightBlue
Next
This error will occur if you do not declare the Form class to be public.
Also, make sure the textbox names are really correct, although this will probably cause a different error.
If you create the textboxes during execution, make sure they are initialized with New and added to the form's Controls collection.
Try this....
Public Sub backblue(ByVal frm As Form, ByVal prefix As String)
For i = 1 To 3
Dim bxName as String = prefix & i.ToString()
Dim bx as TextBox = CType(frm.Controls(bxName), TextBox)
If bx Is Nothing Then
MsgBox("Unable to find text box " +bxName)
Dim mtch() As Control = frm.Controls.Find(bxName, true)
If mtch.Length> 0 then
bx = mtch(0)
Else
Continue For
End if
End If
Bx.BackColor = Color.LightBlue
Next
End Sub
Although, a better solution would be to either create the textboxes inside a control and pass that control to BackBlue or to create an collection that has the controls and pass that in. Which brings up what is most likely yor problem your control is contained in a sub component and thus is not in the main form control collection
Alternative, you could use either the tag of the control or create a component control that implements IExtenderProvider and add it to the form --all of the above would effectively allow you to define the controls and/how they should be handled at designtime.
It may really seem that the names generated by this loop may not be the names of the original textboxes. My suggestion is before setting this Color property verify that the names generated by this loop are indeed the actual names. Maybe output this in a messagebox:
MessageBox.Show(boxname & i.ToString()) for each loop before you set the property

Can not update List Box Selected Collection in code

I have a list box on a form being populated from a query, with items selected on basis of matching a delimited list. So sSystemString equals something like "A;B;C"
Then I load records A,B,C,D,E,F from the SQL Server DB and only A,B,C should be selected.
Is there a native way to do this in MS Access (2010). I'm using an ADP in this case.
I'm doing it via code but I can't the selected property does not reflect my changes, nor does the form.
Here's my code:
Dim rs As New ADODB.Recordset
Dim sSystemString As String
If Not IsNull(Me.OpenArgs) Then sSystemString = Me.OpenArgs
' Load this list box with SRC Systems
Call rs.Open("SELECT DISTINCT System FROM dbo.System WHERE System IS NOT NULL", _
CurrentProject.Connection, adOpenForwardOnly, adLockReadOnly)
Do Until rs.EOF
lstSrcSystems.AddItem (rs.Fields(0))
If InStr(sSystemString, rs.Fields(0)) > 0 Then
lstSrcSystems.Selected(lstSrcSystems.ListCount - 1) = True
End If
rs.MoveNext
Loop
My code definitely hits the lstSrcSystems.Selected(lstSrcSystems.ListCount - 1) = True line.
After running this line, inspecting the property in the immediate window still returns 0 (it doesn't change). On the form, the item is also not selected.
UPDATE: I just checked my code again and now it is being updated, but the next AddItem apparently unselects it again.
I suspect I have some weird combination of properties that make this read only, but I can select items interactively, and indeed when I extract the selected items back off in code, the Selected property works as expected - i.e. I select an item on the form and it reflected in this property.
The form is unbound and is called from a button on another form with this code:
DoCmd.OpenForm "fSiteList", acNormal, , , acFormEdit, acDialog, Me.SRCSystems
The problem was actually that lstSrcSystems.AddItem (rs.Fields(0)) was resetting the Selected state. I can't find any mention of this behaviour or how to turn it off. I altered my form as follows:
Form / Data properties all blank (unbound)
List Control / Data / Control Source: blank
List Control / Data / Row Source: SELECT statement populating my list
This has the effect of populating the list but not binding the list or form to anything. (I found that binding it stopped me interactively editing data on it)
Form_Load code behind the form was changed to select already existing items:
Private Sub Form_Load()
Dim sSystemString As String
Dim iIndex As Integer
' List is bound to site list (from connections)
' Highlight those that are listed
If Not IsNull(Me.OpenArgs) Then sSystemString = Me.OpenArgs
iIndex = lstSrcSystems.ListCount
Do While iIndex > 0
If InStr(sSystemString, lstSrcSystems.ItemData(iIndex)) > 0 Then
lstSrcSystems.Selected(iIndex) = True
End If
iIndex = iIndex - 1
Loop
End Sub
I'm still curious to know whether there is a more 'built in' way to achieve this: edit a delimited string field.

vb2010 combobox automatic change text from items

I am using vb2010 and I have a problem with combobox. My code below get the items from mysql database then add it to the combobox. When there is an item in combobox say for example "NERISON" when I input "N" in the combox and press tab, the combobox will automatically change the text to "NERISON" -what I don't want. I just want to leave it with "N" as text. How would I do that?
If Not e.KeyChar = ChrW(8) Then
txtprice.Text = ""
With cmb_particular
.Items.Clear()
load_dbase() ' connects to database
CNN.Open()
runSql("select particular from particular where status=0 and particular like '%" & .Text & "%' order by particular") ' my function for queries
While dr.Read
.Items.Add(dr("particular"))
End While
CNN.Close()
.SelectionStart = cmb_particular.Text.Length
.DroppedDown = True
End With
End If
Could it be that autofill / autocompelte is enabled in the combobox?
For WPF, set the following:
IsTextSearchEnabled = False
For Forms:
ComboBox.AutoCompleteMode = False
In your combobox properties be sure that AutoCompleteMode=None
#Nerison:
I 've added a comboBox in a form. I don't change anything. I check its properties:
ComboBox1.AutoCompleteMode=None
ComboBox1.AutoCompleteSource=None
ComboBox1.DropDownStyle=DropDown
I add a datatable as datasource. Now I have the bahaviour that you want. I type "N" and it doesn't suggest or append anything.
Could you please check it again?

Visual Basic 2008 Mobile Project: How to Filter Data From Combo Box to DataGrid

I'm working on a mobile project, and I have this:
A search textbox (fillby method)
A combobox (bound to the data)
A datagrid
I am able to do this:
input a search query into the textbox using the fillby method and the datagrid shows the appropriate rows.
I need help with this:
To filter the same data with a combobox. If I use the Add Query method (fillby method) to a combobox it creates another textbox search query. I don't want that. I want to be able to filter the datagrid by the combobox.
Here is my code for the ComboBox Sub:
Private Sub CityComboBox_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CityComboBox.SelectedValueChanged
Dim RestIDShort As Short 'primary key
Dim RestDataRow As DataRow 'complete data row of selected value
Dim RestDataRows As DataRow() 'holding the data
Try
'get the restID for the selected city
RestIDShort = Convert.ToInt16(CityComboBox.SelectedValue)
'find the row from the table for the selected city
RestDataRow = RestaurantEateriesDataSet.RestaurantTable.FindByRestID(RestIDShort)
'Grab the variables here. Don't really need them. Just to see if I can pull data.
'NameStringShow = RestDataRow("Name")
'FoodTypeStringShow = RestDataRow("FoodCat")
'CityStringShow = RestDataRow("City")
'test to see if we can write to screen
'successfully wrote this to the screen onload but not on combobox change
'TextBox1.Text = NameStringShow
'retrieve the array for the selected data row
'not sure if this is how to call when there is only one table????
RestDataRows = RestDataRow.GetChildRows("RestaurantTable")
'fill the datagrid with the array of selected value rows
'I don't know how to do this part:
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
I do have a query created that I can call (if needed). The query works when I call it in a textbox, so if there is a way to call it in a combo box, and then display the selected fields in the datagrid . . . all would be good.
Any help, much appreciated.
Private Sub bttnFilter Click(...) Handles bttnFilter.Click
Dim filter As String
filter = InputBox("Enter product name, or part of it")
ProductsBindingSource.Filter = "ProductName LIKE '%" & filter.Trim & "%'"
End Sub
the same is applied to combobox, use combobox1.selectedItem() in place of the filter.
its the basics of searching, it you have any other question you're welcome.