PatrickStel
PatrickStel

Reputation: 49

ms access dynamically adding textbox for amount of goals

Good evening,

I'm completely stuck in ms access, trying to create dynamically added fields.

I've got a form called frmMatch.

It contains 6 fields called:

MatchDate - Date field
CompetitionType - ComboBox
Location - Input field
TeamName - Input field
ResultHome - Input field
ResultAway - Input field

Now what I would like to create is just a simple button called something like Add Scored Players

The problem is, I have no clue on how to get VBA to run through the ResultHome field if it is a Home game or through ResultAway if it is an Away game.

For example when a home game end up with a 3-1 win I would like VBA to run through a loop until it hits the ResultHome value, in this case 3.

And adds the amount of text box in according with the ResultHome value.

So I can put in the names from a combobox or textbox and the time he scored.

I hope you guys can help me out with this.

Ive searched on this form as well as other website but I cant find anything that would help me.

Upvotes: 0

Views: 381

Answers (1)

Johnny Bones
Johnny Bones

Reputation: 8402

What you need here is not to "add textboxes", but rather to make them visible.

So, what you'll need to do is to create all your textboxes and set their Visible property to "False". Name them with numbers, like "tbScore1", "tbScore2", "tbScore3", etc...

Then, you'll need to add some VBA behind your button to make the proper number of textboxes visible. Something like:

'If both scores are 0, no need to show anything
  If ResultHome.Value = 0 and ResultAway.Value = 0 then
    exit sub
  else
'Otherwise, set the value of the loop to whatever the score is
    If ResultHome.Value <> 0 then
      LoopVal = ResultHome.Value
    else
      LoopVal = ResultAway.Value
    endif
  endif

'Make as many textboxes visible as necessary
  For i = 1 To LoopVal
    MyFormName.Controls("tbScore" & i).Visible = True
  Next

Note: this is all "aircode" and is not tested, so it may require some tweaking to make it work. But this is the logic structure you can use to achieve your results.

Upvotes: 1

Related Questions