Solved

If statments problems in VB6

Posted on 2007-03-20
12
281 Views
Last Modified: 2013-12-20
Hi guys i have something weird happening here and i dont know why, io have done this kind of this 100 times before and never had a issue.

Software is VB6
Database is MSDB

what i have is two if statements (at the moment there is going to be a lot more) if i select the if that matches the first if there is no problem and it works fine, but if i select the if that matches the 2nd one i get an error __ run-time error '5'; in valid procedure call or argument __ i have swoped there order in the code then the one that didnt work works, when i hit debug the it does to the start of the first if statment.  The code is below,  knowing me it is something dumb that i have done

Private Sub Archivedate_Click()
history.ListItems.Clear
connect
      Dim rs As New ADODB.Recordset
      Dim sql As String
      Dim objItem As ListItem
           
       
If sfmt.Tag = True Then
sql = "select * from [Assessment Records] where [date]=#" & Format(Archivedate, "dd/mmm/yyyy") & "#  and  [member id] = " & mid.Caption
Debug.Print sql
cnn1.Open
rs.Open sql, cnn1, adOpenDynamic, adLockPessimistic
   adate.Caption = Format((Archivedate.Text), "DDDD DD/mmm/YYYY")
       
                   Set objlst = history.ListItems.Add(, , bicepssfm.Caption)
                   objlst.SubItems(1) = rs.Fields("bicepssfm")
                   Set objlst = history.ListItems.Add(, , Tricep.Caption)
                   objlst.SubItems(1) = rs.Fields("tricepsfm")
                   Set objlst = history.ListItems.Add(, , subscapular.Caption)
                   objlst.SubItems(1) = rs.Fields("subscapular")
                   Set objlst = history.ListItems.Add(, , supraiiiliac.Caption)
                   objlst.SubItems(1) = rs.Fields("SUpRAiI  iLIAC")
                   history.Visible = True
                   rs.Close
                   cnn1.Close
                   Else
If psword.Tag = True Then
cnn1.Open
sql = "select * from [Assessment Records] where [date]=#" & Format(Archivedate, "dd/mmm/yyyy") & "#  and  [member id] = " & mid.Caption
Debug.Print sql
rs.Open sql, cnn1, adOpenDynamic, adLockPessimistic
       adate.Caption = Format((Archivedate.Text), "DDDD DD/mmm/YYYY")
                    Set objlst = history.ListItems.Add(, , Label55.Caption)
                   objlst.SubItems(1) = rs.Fields("cv capacity")
                   Set objlst = history.ListItems.Add(, , Label54.Caption)
                   objlst.SubItems(1) = rs.Fields("FLEXIBILITY SCORE")
                   Set objlst = history.ListItems.Add(, , Label59.Caption)
                   objlst.SubItems(1) = rs.Fields("ABDOMINAL STRENGTH")
                   Set objlst = history.ListItems.Add(, , Label74.Caption)
                   objlst.SubItems(1) = rs.Fields("CHEST STRENGTH")
                   Set objlst = history.ListItems.Add(, , Label57.Caption)
                   objlst.SubItems(1) = rs.Fields("LEG STRENGTH")
                   Set objlst = history.ListItems.Add(, , Label52.Caption)
                   objlst.SubItems(1) = rs.Fields("HANDGRIP STRENGTH L")
                   Set objlst = history.ListItems.Add(, , Label58.Caption)
                   objlst.SubItems(1) = rs.Fields("HANDGRIP STRENGTH R")
                    Set objlst = history.ListItems.Add(, , Label50.Caption)
                   objlst.SubItems(1) = rs.Fields("SCAPULAR STRENGTH")
                   Set objlst = history.ListItems.Add(, , Label65.Caption)
                   objlst.SubItems(1) = rs.Fields("Back STRENGTH")
                   history.Visible = True
                   rs.Close
                   cnn1.Close
                   Else

         
' rs.Close
'cnn1.Close

End If
End If
End Sub
0
Comment
Question by:awolarczuk
  • 7
  • 5
12 Comments
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
The things that jump out at me (and admittedly, it's late for me... :)

Combine this:
                 Else
If psword.Tag = True Then

into                  

ElseIf psword.Tag = True Then

Also - mid.Caption - is that a control name?  It's also a function, so perhaps alter that to something else...

To clarify - you're saying
If A Then
 'this works
elseif B Then
 'this doesn't
End If

Yet,
If B Then
 'this works
elseif A Then
 'this doesn't
End If

Is that right?
0
 

Author Comment

by:awolarczuk
Comment Utility
yep i am saying if a then fo this if b then do this and then it will get to if c do this in each case it only does one of then, i have change it to else if and it didnt make a difference
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Oh, well that changes things slightly, I think...
Let's weed out everything except your comparisons...
       
If sfmt.Tag = True Then
 'if sfmt.Tag is "True" then process this code...
  Else  'If it's not then test the code here
    If psword.Tag = True Then
       'If psword.Tag is "True" then process this code
    Else
       'otherwise process this
    End If
End If

So, I'm not sure that the comparisons are setup properly.
Try it this way

       
If sfmt.Tag = True Then
 'A
ElseIf psword.Tag = True Then
 'B
Else
 'C (will only process if A & B are not True)
End If
0
 

Author Comment

by:awolarczuk
Comment Utility
yea almost when i am in the software i am going to be calling a,b or c none of them can be on at the same timebut any of them can be selected at anytime, what you have above is what i already am doing
0
 

Author Comment

by:awolarczuk
Comment Utility
Private Sub Archivedate_Click()
history.ListItems.Clear
connect
      Dim rs As New ADODB.Recordset
      Dim sql As String
      Dim objItem As ListItem
           
       
If sfmt.Tag = True Then
sql = "select * from [Assessment Records] where [date]=#" & Format(Archivedate, "dd/mmm/yyyy") & "#  and  [member id] = " & mid.Caption
Debug.Print sql
cnn1.Open
rs.Open sql, cnn1, adOpenDynamic, adLockPessimistic
   adate.Caption = Format((Archivedate.Text), "DDDD DD/mmm/YYYY")
       
                   Set objlst = history.ListItems.Add(, , bicepssfm.Caption)
                   objlst.SubItems(1) = rs.Fields("bicepssfm")
                   Set objlst = history.ListItems.Add(, , Tricep.Caption)
                   objlst.SubItems(1) = rs.Fields("tricepsfm")
                   Set objlst = history.ListItems.Add(, , subscapular.Caption)
                   objlst.SubItems(1) = rs.Fields("subscapular")
                   Set objlst = history.ListItems.Add(, , supraiiiliac.Caption)
                   objlst.SubItems(1) = rs.Fields("SUpRAiI  iLIAC")
                   history.Visible = True
                   rs.Close
                   cnn1.Close
                 
ElseIf psword.Tag = True Then
cnn1.Open
sql = "select * from [Assessment Records] where [date]=#" & Format(Archivedate, "dd/mmm/yyyy") & "#  and  [member id] = " & mid.Caption
Debug.Print sql
rs.Open sql, cnn1, adOpenDynamic, adLockPessimistic
       adate.Caption = ""
       adate.Caption = Format((Archivedate.Text), "DDDD DD/mmm/YYYY")
                    Set objlst = history.ListItems.Add(, , Label55.Caption)
                   objlst.SubItems(1) = rs.Fields("cv capacity")
                   Set objlst = history.ListItems.Add(, , Label54.Caption)
                   objlst.SubItems(1) = rs.Fields("FLEXIBILITY SCORE")
                   Set objlst = history.ListItems.Add(, , Label59.Caption)
                   objlst.SubItems(1) = rs.Fields("ABDOMINAL STRENGTH")
                   Set objlst = history.ListItems.Add(, , Label74.Caption)
                   objlst.SubItems(1) = rs.Fields("CHEST STRENGTH")
                   Set objlst = history.ListItems.Add(, , Label57.Caption)
                   objlst.SubItems(1) = rs.Fields("LEG STRENGTH")
                   Set objlst = history.ListItems.Add(, , Label52.Caption)
                   objlst.SubItems(1) = rs.Fields("HANDGRIP STRENGTH L")
                   Set objlst = history.ListItems.Add(, , Label58.Caption)
                   objlst.SubItems(1) = rs.Fields("HANDGRIP STRENGTH R")
                    Set objlst = history.ListItems.Add(, , Label50.Caption)
                   objlst.SubItems(1) = rs.Fields("SCAPULAR STRENGTH")
                   Set objlst = history.ListItems.Add(, , Label65.Caption)
                   objlst.SubItems(1) = rs.Fields("Back STRENGTH")
                   history.Visible = True
                   rs.Close
                   cnn1.Close
               

         
' rs.Close
'cnn1.Close

End If
End Sub
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Mid.Caption
Does this not throw an error?  It does on my end...what is Mid?
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Also, your last post - it doesn't contain the fall out Else statement...
If A Then
ElseIf B Then
Else 'presumably C
End If

Also - your test for the Tag property...they're not boolean.  I assume you mean to enclose these in quotes:

If sfmt.Tag = "True" Then
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
This is the output from the code below...

sfmt.tag is true
psword.tag is true
Neither tags are True


Sub form_Load()
sfmt.Tag = "True"
Archivedate_Click
sfmt.Tag = ""
psword.Tag = "True"
Archivedate_Click
psword.Tag = ""
Archivedate_Click
End
End Sub

Private Sub Archivedate_Click()
If sfmt.Tag = "True" Then
  sql = "select * from [Assessment Records] where [date]=#" & Format(Archivedate, "dd/mmm/yyyy") & "#  and  [member id] = " '& Mid.Caption
  Debug.Print "sfmt.tag is true"
ElseIf psword.Tag = "True" Then
  sql = "select * from [Assessment Records] where [date]=#" & Format(Archivedate, "dd/mmm/yyyy") & "#  and  [member id] = " '& Mid.Caption
  Debug.Print "psword.tag is true"
Else
  Debug.Print "Neither tags are True"
End If
End Sub
0
 

Author Comment

by:awolarczuk
Comment Utility
ok i got it working it was the fact that i didnt have the "" are the true, can you please tell me whys this is so
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
Comment Utility
True is typically used for boolean values...

Dim blnTest As Boolean

If blnTest Then
or
If blnTest=True Then

are both valid, because the boolean can only be True or False

However, you're using a string value for the Tag property (which won't accept a boolean), so you have to specify that it's a string, by enclosing it in quotes in your test... if control.Tag="Some value" 'could be anything - I usually drop a numeric character in there, rather than text....

Glad that worked for you! :^)
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Not sure why this was graded a "B".  What was missing from my answer?
0
 

Author Comment

by:awolarczuk
Comment Utility
I didnt think it wasa bad answer but i didnt think it was the best one that i have got on the site, even though the elseif cleaned up the code, it wasnt needed to fix the problem that i was having, the fix for that was just the "" and i feel that it took some time to get to this Answer, i am sorry for the grade
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now