Results 1 to 8 of 8

Thread: combobox and array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Columbus, OH
    Posts
    22
    Hello all...

    I have a combobox in Form1 and want to populate the
    selected item in Form2... I tried coding it with an
    array and it didn't work... can anyone tell me what
    i did wrong??

    Here's my code:
    Form1 = frmComputerSales
    Form2 = frmReceipt

    Private Sub cmbHardDrive_Click()

    strHardDrive = Array("", "10 GB", "20 GB", "30 GB", "40 GB")
    frmReceipt.lblHardDrive.Caption = strHardDrive
    (frmComputerSales.cmbHardDrive.ListIndex)
    End Sub

    Thanks,

    Eena
    Eena

  2. #2
    Addicted Member
    Join Date
    Aug 1999
    Location
    Hamilton, New Zealand
    Posts
    137
    1. Paste the following code into your frmComputerSales form
    2. Ensure that you have a combo box called 'cmdHardDrive' on frmComputerSales. It doesn't matter if there is anything in it because the form's loading code will clear it and then fill it from the array.
    3. Ensure that you have a label called 'lblHardDrive' on frmReceipt

    I didn't really change any of your code, except to make strHardDrive a form level variable. I then added the code to fill the combo box, because it looks like you are relying on the combo having an item from the array in it? Therefore you need to ensure that the combo only contains the items in the array.

    Code:
    Option Explicit
    Dim strHardDrive
    
    Private Sub cmbHardDrive_Click()
        frmReceipt.Show 
        frmReceipt.lblHardDrive.Caption = strHardDrive(cmbHardDrive.ListIndex)
    End Sub
    
    Private Sub Form_Load()
        
        Dim i
        
        'Create the array
        strHardDrive = Array("", "10 GB", "20 GB", "30 GB", "40 GB")
        'Add the items to the combo box
        cmbHardDrive.Clear
        For i = LBound(strHardDrive) To UBound(strHardDrive)
            cmbHardDrive.AddItem strHardDrive(i)
        Next
    End Sub
    Hope this helps
    Regards

    Matt Brown
    Hamilton, NZ
    VB6 C++ in Visual Studio 6sp4
    VB.net Beta 1

  3. #3
    Guest

    Question <?>

    What happened? Did you get an error? Did the label display the wrong item or no item at all? What makes it "not work"?

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Escaflowne

    'there is nothing wrong with the code other than a brief
    'explantaion for you and 1 or 2 personal little things I stuck in it

    Code:
    'needed 2 forms  frmHardDrive   frmReceipt
    'needed on frmHardDrive  combobox called cmbHardDrive
    'needed on frmHardDrive  command button called cmdHardDrive
    '
    'needed on frmReceipt
    'command button called cmdReceipt
    'label called lblHardDrive
    
    'Copy and paste this code into frmHardDrive
    
    Option Explicit
    
    Dim strHardDrive
    
    Private Sub cmbHardDrive_Click()
        frmReceipt.Show
        frmReceipt.lblHardDrive.Caption = strHardDrive(cmbHardDrive.ListIndex)
    End Sub
    
    Private Sub Form_Load()
        
        Dim i
        
        'Create the array
        strHardDrive = Array("Hard Drive Size", "10 GB", "20 GB", "30 GB", "40 GB")
        'Add the items to the combo box
        cmbHardDrive.Clear
        For i = LBound(strHardDrive) To UBound(strHardDrive)
            cmbHardDrive.AddItem strHardDrive(i)
        Next
        
      'set the first item in the text box for visual
        cmbHardDrive.ListIndex = 0
        
    End Sub
    
    '
    'copy and paste this code into frmReceipt
    
    Option Explicit
    
    Private Sub cmdReturn_Click()
        Unload frmReceipt
        frmHardDrive.Show
    End Sub
    
    Private Sub Form_Load()
        cmdReturn.Caption = "Return"
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5
    Guest

    Argh!

    HeSaidJoe, I already knew that!

    To think that I'm a advanced programmer who doesn't even know the basics? How do you think that I go around the forums without even knowing that?

    I was asking EENA what does she mean by "It does not work" and not correcting mattb's code. I didn't even read his code until after I've posted mine.

    I'm not being mean. No offence.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Columbus, OH
    Posts
    22
    Hello... and thanks for your responses...

    I tried applying the codes you guys gave me
    and it gave me and "array expected" error msg...

    Also, i forgot to mention that at the click of the cmdOrder button (in form1), the caption of the selected item in the combobox should populate the lblHardDrive (in form2).

    I have a module that holds all the variables. I have the strHardDrive declared as string...

    Code in Form1:

    Private Sub form_Load()

    cmbHardDrive.AddItem ""
    cmbHardDrive.AddItem "10 GB"
    cmbHardDrive.AddItem "20 GB"
    cmbHardDrive.AddItem "30 GB"
    cmbHardDrive.AddItem "40 GB"
    end sub

    Private Sub cmbHardDrive_Click()

    strHardDrive = Array("", "10 GB", "20 GB", "30 GB", "40 GB")
    frmReceipt.lblHardDrive.Caption = strHardDrive(frmComputerSales.cmbHardDrive.ListIndex)
    End Sub

    Thanks,
    Eena

  7. #7
    Addicted Member
    Join Date
    Aug 1999
    Location
    Hamilton, New Zealand
    Posts
    137
    If you have the strHardDrive declared as a string, not a string array, that is where your problem is.

    your declaration of strHardDrive should be

    Dim strHardDrive

    The array() function expects to return to a variant I believe and declaring to a string will definetely cause problems.

    You could try declaring strHardDrive as a string array

    ie

    Dim strHardDrive() as string

    and see if that works. I'm not too sure on the specifics of what array() expects but that will be where the problem is.

    Maybe someone else can expand on this?
    Regards

    Matt Brown
    Hamilton, NZ
    VB6 C++ in Visual Studio 6sp4
    VB.net Beta 1

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2000
    Location
    Columbus, OH
    Posts
    22
    Hello all...

    Sorry for the delayed response... I just got back after taking my daughter to ER... Viral epedimic going around in school... and change of weather...

    I brought my laptop with me (which was handy and kept me company while sitting in the ER for 5 hrs) and played around with the codes you guys sent me and i got it to work...

    Thanks for all your help... I really appreciate it...



    Eena

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width