Hello aLL

I need a small help from you guys..
I am creating project in VB6.0 and access 2003, form has combobox,tab control,textbox msflexgrid .. I am trying to retrive data from database.I have a sub procedure which is nicely working for all controls except combobox.
while debugging I found that when cursor goes to this field cursor exists sub.

cmblocation.text=rsget!location
or 
cmblocation.text=trim(rsget!location)& ""

I dont know where i am going wrong.please help me in this...

thanks in advance

Dani AI

Generated

A few likely causes and a short checklist to get the combo working reliably.

The assignment to cmbLocation.Text can fail for a few different reasons: the Recordset field may be Null, the ComboBox’s Style may be set so the text area is not editable, or the Text property is being set at a time the control doesn’t have focus/is not ready. ’s comment about using an index points in the right direction — the property used to select an item is the combo’s ListIndex (Index is for control arrays), so set the item by index instead of trying to force Text when the control won’t accept it. ’s suggestion to populate the control with a loop is sensible; just add a couple of safety checks (below).

Practical checklist (follow in order)

  1. Verify the recordset actually contains a value before touching the combo. Inspect rs.Fields("location").Value in the debugger. Test IsNull and handle it explicitly rather than assuming Trim/other string functions will cope.
  2. If the combo’s Style is “dropdown list” make selection with ListIndex (or search the existing list for a match), not by assigning Text. If you must set Text, ensure the control can accept it (editable style or has focus).
  3. Clear the combo before populating to avoid duplicates. Add items from the recordset, handling Nulls (convert to "" or skip), then set ListIndex to the desired row.
  4. If you only need a display string and an ID, keep the ID separately (ItemData or a parallel array) so selection and storage don’t conflict.

Troubleshooting tips

  • Use Debug.Print on the field value and VarType to see Null vs. empty string.
  • Wrap field reads with an explicit IsNull/Value check to avoid runtime errors.
  • If the combo is on a Tab/MultiPage, populate it after that page/control is initialized.

These steps will catch the common causes that make the sub exit at the combo assignment.

Recommended Answers

All 4 Replies

Change the

cmbLocation.Text" 'part to cmbLocation.Index 'and the index number

As what I understand, you mean is you're trying to populate the combo box, with data coming from the database,

there are many ways you can load data within a database...

1.) open a recordset object
rs.Open "SELECT * FROM myTable"
2.) then go through the all the records...with a loop

Dim ctr as Integer
Dim i as Integer 
'-> you can make the data type larger than the integer type, like the Double data type if you think that the records could make over a number larger than the integer range..

i = rs.RecordCount
For ctr = 0 to i -1
 Me.Combobox1.AddItem rs(ctr), ctr
 rs.MoveNext
DoEvents '-execute other events, this avoids to make your program to hang up...
Next i

' for me, this code is a lot faster, than using a "Do While rs.EOF<>True" thing...

About the searching for a current item within the combo box, well as you may know, it will depend on the "Style" property of the combo box, if it's style is set to '2' then the "Text" property will be in a read only mode and thus not allowing you to change the selected item,

you can use an API to search within the combo box

Private Const CB_FINDSTRING = &H14C 'declare this to the top of your form module
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" ( _
    ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Integer, _
    ByVal lParam As Any) As Long

'then paste this to a sub procedure, preferably on button click or text box change event
    ComboBox1.ListIndex = SendMessage(ComboBox1.hwnd, CB_FINDSTRING, -1, _
        ByVal CStr("YOUR SEARCH STRING HERE!!")) '-> you can replace this string with a textbox control's text property

Thanks guys..it helped me alot..!!

It was a pleasure. Please mark this as solved, thanks.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.