Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selecting a value from a Combobox at runtime 1

Status
Not open for further replies.

Overdoos

IS-IT--Management
May 31, 2000
79
BE
Hi,

Once again I find myself struggling with the VB6-code.

This is the situation:
I've got 2 dropdowns (both comboboxes style: 2 - Dropdown List). 1 lists the zip codes, the other the locations.
Whichever of those 2 gets a clickevent first will trigger the other to limit the possibilities to those that match. (so, when you choose a zip-code, the other dropdown will only show the matching locations. On the other hand, if you choose a location, the zip-dropdown will only show the matching zip-code.) This part works like a charm and I'm quite happy with the result I got here.

The problem starts with the 'update' function I need in that screen. This stuff is all part of the customer-data screen and as you know, customers may choose to move. Thus... it has to be able to represent this in the db that runs in the back. Again no problem there, but when you start to edit and you happen to change either the zip and/or the location and suddenly don't want to go through with the edit things go bad.
Upon pushing the cancel button, I restore the whole screen to the previous values, but the location-dropdown gives me a 383 error saying the combobox is read-only.
I -know- that's what I told it to be, but I would still like to get the old value in there when pushing cancel.

Anybody got some ideas?

Code:
Public Sub FillAdresScreen(ByVal state As eEmptyFields)
  With <myForm>
    If state Then
      .txtStreet.Text = ""
      .cmbZip.Text = ""
      .cmbLocation.Text = ""
    Else
      On Error GoTo ErrorHandling
      .txtStreet = thisAbo.strStreet
      .cmbZip.Text = thisAbo.intZip  'zip is a 4-digit code
      .cmbLocation.Text = thisAbo.strLocation
    End If
  End With
  Exit Sub
  
ErrorHandling:
  With frmDvmMain
    Select Case Err.Number
      Case 383
        MsgBox "error occured"
        .cmbLocation.Text = thisAbo.strLocation
    End Select
  End With
  Resume Next
End Sub

-> thisAbo is a TYPE set up the same way as a DB record and it holds the data of the 'current customer'.
-> state is a boolean variable telling the code to blank the screen or restore the data from the 'current customer'.
 
You will have to loop through the combo box items and compare them. When you find a match you can set the listindex property. Something like this.

Code:
Dim i as integer

For i=o to Combo1.listcount-1
  if <myString> = Combo1.list(i) then
    Combo1.listindex = i
    exit for
  end if
next i

zemp
 
When the combobox is set as 2-Dropdown list and you try to assign a value to the text property, the value must match an item already in your combo box list. Otherwise you get the Read Only error. So it seems that cmbLocation is not populated correctly when you assign thisAbo.strLocation. Keep in mind that when you assign a value to the text property of a combobox, the click event will fire
 

Ooops my fault was thinking style = 0 or 1...

Ok, I'll be quiet for now...

Good Luck

 
Thank you all for your help.

It is usually so obvious that I miss it :). Jjames you where right, the combo wasn't populated corretly, so I changed the error-section. Now, whenever I get a 'read-only' error, I restock the dropdown before I try again. This seems to work.

Code:
ErrorHandling:
  With frmDvmMain
    Select Case Err.Number
      Case 383
        if blnDebug then
          MsgBox "error occured"
        end if
        ' Function that fills the combo-box with values that are like the argument.
        ' % is the SQL wildcard
        FillLocationDropDown "%"
        .cmbLocation.Text = thisAbo.strLocalition
    End Select
  End With
  Resume Next
End Sub

the rest of the code remained unchaged for now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top