Helppppp (If Statement) (1 Viewer)

Has this problem ever happened to you?


  • Total voters
    1
  • Poll closed .

taicho

Registered Abuser
Local time
Yesterday, 23:28
Joined
Dec 13, 2005
Messages
24
Alright my friends I am in need of help again...I just can't figure this out I tried "If" "Select" "Goto" everything and my brain can't work it out...

I have 10 unbound controls named (NOne,NTwo,NThree, etc...) what I want is this- user clicks button to add the value in ControlX too NOne but if NOne already has a value in it from clicking the button then it adds it to NTwo but if NTwo has a value in it from clicking the button then it adds it to NThree etc...basically they would click to add values to a "list" which are really controls next to each other...Almost like adding stuff to a "Shopping Cart"

Help please my brain hurts!!
 
Last edited:

taicho

Registered Abuser
Local time
Yesterday, 23:28
Joined
Dec 13, 2005
Messages
24
Got it nevermind thanks though!!!!
 

WalterInOz

Registered User.
Local time
Today, 16:28
Joined
Apr 11, 2006
Messages
93
taicho said:
Got it nevermind thanks though!!!!

Would be nice if you showed us how you've solved it. Others (like me) might learn from it.
 

taicho

Registered Abuser
Local time
Yesterday, 23:28
Joined
Dec 13, 2005
Messages
24
WalterInOz said:
Would be nice if you showed us how you've solved it. Others (like me) might learn from it.
My apologies, the way I ended up solving it was instead of trying to incorporate ALL of the code into an If..Then..Else statement I made the Else part goto a seperate Procedure instead of being in the same one here is a bit of the code:


Code:
Private Sub Command131_Click()
If IsNull(Me.NOne) Then
Me.NOne = Me.PartNumber
Else
NnTwo
End If
End Sub
Public Sub NnTwo()
If IsNull(Me.NTwo) Then
Me.NTwo = Me.PartNumber
Else
NnThree
End If
End Sub
Public Sub NnThree()
If IsNull(Me.NThree) Then
Me.NThree = Me.PartNumber
Else
NnFour
End If
End Sub
Public Sub NnFour()
If IsNull(Me.NFour) Then
Me.NFour = Me.PartNumber
Else
NnFive
End If
End Sub
Public Sub NnFive()
If IsNull(Me.NFive) Then
Me.NFive = Me.PartNumber
Else
NnSix
End If
End Sub
 
Last edited:

MarkK

bit cruncher
Local time
Yesterday, 23:28
Joined
Mar 17, 2004
Messages
8,186
Observation:
If you name your controls like "N1", "N2", ... "N8" etc... you can write one loop that traverses them all, rather than multiple and practically identical routines.
Consider:
Code:
Private Const conMaxControls = 8

Private Sub Command131_Click()
  AssignPartToControl
End Sub

Private Sub AssignPartToControl(Optional num As Integer = 1)
[COLOR="SeaGreen"]'  Recursive routine that assigns Me.PartNumber to the first available control[/COLOR]
[COLOR="SeaGreen"]  'make sure there is a control to assign to[/COLOR]
  If num <= conMaxControls Then
[COLOR="SeaGreen"]    'reference the control via the controls collection of the form[/COLOR]
    With Me.Controls("N" & num)
[COLOR="SeaGreen"]      'if it is null then ...[/COLOR]
      If IsNull(.Value) Then
[COLOR="SeaGreen"]        'assign the part number[/COLOR]
        .Value = Me.PartNumber
      Else
[COLOR="SeaGreen"]        'otherwise, recall this function addressing the next control[/COLOR]
        AssignPartToControl num + 1
      End If
    End With
  Else
[COLOR="SeaGreen"]    'and if there are no more controls, send a warning[/COLOR]
    MsgBox "There are no controls left to assign part number to"
  End If

End Sub

But having multiple part numbers in one record suggests to me that you should instead be using a second table on the many side of a one-to-many relationship. Then you simply add the part number to the "many" table with a link to the record in the "one" table.
 

taicho

Registered Abuser
Local time
Yesterday, 23:28
Joined
Dec 13, 2005
Messages
24
lagbolt said:
Observation:
If you name your controls like "N1", "N2", ... "N8" etc... you can write one loop that traverses them all, rather than multiple and practically identical routines.
Consider:
Code:
Private Const conMaxControls = 8

Private Sub Command131_Click()
  AssignPartToControl
End Sub

Private Sub AssignPartToControl(Optional num As Integer = 1)
[COLOR="SeaGreen"]'  Recursive routine that assigns Me.PartNumber to the first available control[/COLOR]
[COLOR="SeaGreen"]  'make sure there is a control to assign to[/COLOR]
  If num <= conMaxControls Then
[COLOR="SeaGreen"]    'reference the control via the controls collection of the form[/COLOR]
    With Me.Controls("N" & num)
[COLOR="SeaGreen"]      'if it is null then ...[/COLOR]
      If IsNull(.Value) Then
[COLOR="SeaGreen"]        'assign the part number[/COLOR]
        .Value = Me.PartNumber
      Else
[COLOR="SeaGreen"]        'otherwise, recall this function addressing the next control[/COLOR]
        AssignPartToControl num + 1
      End If
    End With
  Else
[COLOR="SeaGreen"]    'and if there are no more controls, send a warning[/COLOR]
    MsgBox "There are no controls left to assign part number to"
  End If

End Sub

But having multiple part numbers in one record suggests to me that you should instead be using a second table on the many side of a one-to-many relationship. Then you simply add the part number to the "many" table with a link to the record in the "one" table.
Thank you very much for your suggestion lagbolt, just goes to show me I have so much more to learn, I am going to implement this method instead of my other one, I wanted to mention though that I am not actually storing these numbers they are being listed in controls purely for routing to a report that has query with an criteria that asks for those control names for the value.

I will try this one though, and thank you once again!
 

Users who are viewing this thread

Top Bottom