Avatar billede Slettet bruger
19. juli 2002 - 17:13 Der er 6 kommentarer og
2 løsninger

Alternativ til indexes til VBA objekter.

Hej allesammen.

Jeg skal lave et spørgeskema med x antal comboboxes.

Der skal desuden være change-event på samtlige combos (identiske db-opslag)

Jeg opretter comboboxene vha. OLEObject.add()... men har ikke mulighed for at definere et index.
Ved derfor ikke hvordan dette kan løses.


Lad os sige der er 50 comboboxes...

for i = 1 to 50
  sub combo(i)_change ()

  ..........

  end sub
next

Ved godt dette ikke vil virke, men håber I kan se pointen ;)



Håber I kan knække nødden ;)


Med venlig hilsen

Michael
Avatar billede terry Ekspert
19. juli 2002 - 17:38 #1
Not QUITE sure it is your trying to do so it isnt easy to give a solution.
WHY are you trying to call all 50 combo boxes CHANGE event in a loop!
Avatar billede terry Ekspert
19. juli 2002 - 17:43 #2
you can make an array of combo boxes! Make on ethen copy it and you will be asked if you want to make an array. Answer YES and you will get a change event with an index which is the number of the combo in the array!
Avatar billede terry Ekspert
19. juli 2002 - 17:52 #3
If you ARE trying to call ALL X (50) CHANGE events then I assume that you have also CODE in the event procedure which is different for each combo. If that is correct then you HAVE concidered what code is needed for each. So you could just as wel make one function and call that with a number

for i = 1 to 50
  YourFunction(i)

next i

PUBLIC FUNCTION YourFunction(i as integer)

  SELECT CASE i
      CASE 1
      ...
      CASE 2
      ...
      .
      .
      CASE ELSE
      ...
    END SELECT
     


END FUNCTION
Avatar billede Slettet bruger
21. juli 2002 - 12:27 #4
Hi Terry.

I'll continue in english... ;)

I already know how to insert combo objects on fly.
It can be done by using the following code:
topvalue = 60
for i = 1 to 50
ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, DisplayAsIcon:=False, Left:=114, Top:=topvalue, Width:=180, Height:=13.5).Select
topvalue = topvalue + 20
next i

--- SNIP --

but how do I create one change-event to all of them, by using one sub, when I'm not able to use the index function ?

I have to make all this as dymaical as possible, because the user shall be able to change the number of combos to a self-chosen number.

Hope you get my point ;)
Avatar billede terry Ekspert
21. juli 2002 - 14:33 #5
If we are talking Excel then I have no idea!
Avatar billede Slettet bruger
21. juli 2002 - 14:52 #6
Hi again.

I just grep the macro code out of Excel.
When talking about VBA it doesn't matter if we are in a excel or a word environment =)
Avatar billede terry Ekspert
21. juli 2002 - 16:19 #7
HI newage
I havent worked with VBA very much in Excel or Word, and in Access which I have worked with quite a lot it isnt quite the same!

In Access I would do something on these lines.

Sub NewControls()
    Dim frm As Form
    Dim ctlLCombo As Control
    Dim i As Integer
   
    Dim intX As Integer, intY As Integer

    intX = 1000
    intY = 100
   
    DoCmd.OpenForm "frmNewControls", acDesign
   
    For i = 1 To 5
        Set ctlCombo = CreateControl("frmNewControls", acComboBox, , "", "", intX + (i * 100), intY + (i * 100))
       
        'Name the combo
        ctlCombo.Name = "cbo" & i
        'Set the OnClick event to call MyFunction

        ctlCombo.OnClick = "=MyFunction(" & i & ")"
       
    Next i
   
    DoCmd.Close acForm, "frmNewControls", acSaveYes
   
End Sub

The MyFunction looks like this

Public Function MyFunction(Index As Integer)
    MsgBox "Index = " & Index
   
End Function
Avatar billede Slettet bruger
16. august 2002 - 19:28 #8
I close this thread.

Seems not to be a solution anywhere on then net ;(

So... I'll split the point 50% / 50%.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester