Results 1 to 20 of 20

Thread: [RESOLVED] Help with codes!

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2014
    Location
    In my own world
    Posts
    50

    Resolved [RESOLVED] Help with codes!

    I want to now that why doesn't label7 display the Rate when I click Command 3. Label 7 has an integer data from an access database via ADO controller..
    Private Sub Command3_Click()
    units = Label1.Caption.Value
    Select Case units
    Case Is > 250
    Rate = 3
    Case Is <= 250
    Rate = 2
    Case Is <= 150
    Rate = 1
    End Select
    Label7.Caption = Rate
    End Sub

  2. #2
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Help with codes!

    1-what is Units declared as? String? Int? Double?
    2-label1.caption is a string...., there is no such thing as label1.caption.value....if you want the INTEGER value of that label, do this:

    Code:
    dim units as integer
    units = Cint(label1.caption)
    ....

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Help with codes!

    There are few problems with your code:

    1. Label1.Caption.Value is not a valid syntax; correct syntax is:
    Units = Val(Label1.Caption) or Units = CInt(Label1.Caption) or Units = CLng(Label1.Caption)

    2. You are using Case incorrectly:

    - because 150 < 250 you will get Rate = 2 as long for any number of units between 1 and 250
    - correct syntax may look like this:
    Case 151 To 250
    - you may also use If - End If logic instead of Select Case.

    3. There is no variable declaration (at least thy are not in the posted code).

    4. Here is the corrected sample code:
    Code:
    Option Explicit
    
    Private Sub Command3_Click()
    Dim Units As Long
    Dim Rate As Integer
    
        Units = Val(Label1.Caption)
        
        Select Case Units
            Case Is > 250
                Rate = 3
            Case 151 To 250
                Rate = 2
            Case Is <= 150
                Rate = 1
        End Select
        
        Label7.Caption = Rate
    
    End Sub

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Help with codes!

    Arsalan...did you notice Rhino's use of Option Explicit? You REALLY need this in every project. MAKES you declare CONSTANTS and variables....will save you lots of headaches. Also, as you type in the IDE, something like label1.caption, and then enter another period and nothing shows up for options, then you have the incorrect syntax. (As we both stated above). Do you have a book on VB6? You probably need to look up Variable declarations and the use of the Select Case statement.
    Also, note that Rhino posted near the end, Label7.caption = Rate. In ding so, VB6 will convert that integer Rate, to a String because label captions are always Strings. To be more precise (or for READING anyway), you could use label7.caption = CStr(Rate). CStr() also converts, in this case, an integer to a String. Look at this link for a better explanation of CStr(). http://www.chennaiiq.com/developers/...tions/cstr.asp

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Help with codes!

    Two Notes:
    1- This line units = Label1.Caption.Value must stop the program with compile error.

    2- Rate will never be 1 because any value less than 150 is also less than 250, therefore Case Is <= 250 will be true and Case Is <= 150 will ignored, to solve check for <=150 before <=250

    Code:
    Private Sub Command3_Click()
      units = Label1.Caption.Value
      Select Case units
      Case Is > 250
        Rate = 3
      Case Is <= 150
        Rate = 1
      Case Is <= 250
        Rate = 2
      End Select
      Label7.Caption = Rate
    End Sub



  6. #6
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Help with codes!

    @RhinoBull

    Sorry, didn't see your post before posting my answer!



  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Help with codes!

    Quote Originally Posted by SamOscarBrown View Post
    Arsalan...did you notice Rhino's use of Option Explicit? You REALLY need this in every project.
    He can make the IDE add it in automatically too! Just go to the "Tools" menu then go to "Options" under the "Editor" tab check "Require Variable Declaration", click "OK" ten start a new project.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  8. #8
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Help with codes!

    I would actually write the code as
    Code:
    Private Sub Command3_Click()
    Dim Units As Long
    Dim Rate As Integer
    
        Units = Val(Label1.Caption)
        
        Select Case Units
            Case Is > 250
                Rate = 3
            Case Is >= 150
                Rate = 2
            Case Else
                Rate = 1
        End Select
        
        Label7.Caption = Rate
    
    End Sub

  9. #9

    Thread Starter
    Member
    Join Date
    Feb 2014
    Location
    In my own world
    Posts
    50

    Re: Help with codes!

    @SamOscarBrown and @Rhinobull
    Thankyou for pointing out my mistakes I'm really new to programming and have to meet the deadline of this school project, so I'm doing things the haphazard way . But my VB learning source was using label.caption.value! I'm certain of that! What does "Option Explicit" do? Could you people share some good VB links that teach step by step? About my case statement, guess my frustration had closed my mind to my mistake
    Thankyou @MarkT, @Nightwalker83, @4x2y for your time and help
    P.S I'm new to this forum too so I don't know how to tag people

  10. #10
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Help with codes!

    So many smileys
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  11. #11

    Thread Starter
    Member
    Join Date
    Feb 2014
    Location
    In my own world
    Posts
    50

    Re: Help with codes!

    Is it um.. like a taboo here?..

  12. #12
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: Help with codes!

    personally i prefer if ... elseif....end if block .like the following .
    Code:
    Private Sub Command3_Click()
    Dim Units As Long
    Dim Rate As Integer
    
        Units = Val(Label1.Caption)
     if Units > 250 then
        rate=3
     elseif units >=150 then
        rate=2
      else
       rate=1
     End if
    Label7.Caption = Rate
     End sub

  13. #13
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Help with codes!

    Since efficiency doesn't seem to be a priority, the OP may want to consider the Switch function as an acceptable substitute for the Select Case statement:

    Code:
    Private Sub Command3_Click()
        Dim Units As Integer
    
        Units = CInt(Label1.Caption)
        Label7.Caption = Switch(Units <= 150, 1, Units <= 250, 2, Units > 250, 3)
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  14. #14
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Help with codes!

    Bonnie--you are always amazing me. Switch function? NEVER heard of it! Amazing. You must have ALL of VB in a cache in your head somewhere. Whew!

  15. #15
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Help with codes!

    Quote Originally Posted by SamOscarBrown View Post
    Bonnie--you are always amazing me. Switch function? NEVER heard of it! Amazing. You must have ALL of VB in a cache in your head somewhere. Whew!
    You're not alone. I've never come across that either in all the years I used VB6. Reminds me of IIf.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  16. #16
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Help with codes!

    I always try avoiding using any in-line functions because they could be "dangerous" to use and here is why:

    all arguments must be evaluated first and if any of them generate unhandled runtime error entire functions fails.
    Example:
    a = IIf(adoRst.EOF, "n/a", adoRst!Field1) OR a = IIF(IsNull(adoRst.Field1), "n/a", adoRst!Field1)

    If adoRst.EOF is True or adoRst.Field1 is Null IIf function will fail - to avoid this all conditions must be handled before call is made.

    IIf is a "shotcurt" for If - Else - End If but the second will not fail because it's checking for Null (etc) before assigning value.

    Not sure if Switch works the same way as IIf but I wouldn't be surprised.

  17. #17
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: Help with codes!

    Indeed, all parameters to any function/subroutine/property will be evaluated first before being passed. In the OP's case though, the expressions are simple (and safe) enough that any overhead is most likely unnoticeable. I usually prefer the Select Case statement whenever possible, but in this case, I wanted to offer an (obscure) alternative.
    Last edited by Bonnie West; Feb 13th, 2014 at 01:12 PM.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  18. #18

    Thread Starter
    Member
    Join Date
    Feb 2014
    Location
    In my own world
    Posts
    50

    Re: Help with codes!

    Hey, sorry to disturb again.. But this code of mine was working till yesterday.. Today, i tried tweaking my access database but reverted it back. Now this code is not working idk why.. I've confirmed that all fields are present in the ADODC Recordsource Table.. All the Adodc1.Recordset lines are not working, error displayed is "Runtime error 91. Object variable or with block variable not set."
    Secondly I'd like to know that how can I make a login form that verifies usernames and passwords from an access database. I think I'll have to use ADODC as database is in accdb format...
    I have a datagrid with ADODC source. Is there a way to filter out the records by selecting a particular record?
    Private Sub Command2_Click()
    confirm3 = MsgBox("Are you sure you want to update data?", vbYesNo, "Confirmation")
    If confirm3 = vbYes Then
    Adodc1.Recordset.Fields("Customer_ID").Value = Form4.Label1.Caption
    Adodc1.Recordset.Fields("Month_ID").Value = Form4.Label7.Caption
    Adodc1.Recordset.Fields("Consumption").Value = Form4.Label1.Caption
    Adodc1.Recordset.Fields("Rate_ID").Value = Label1.Caption
    Adodc1.Recordset.Fields("Billing_Amount").Value = Label2.Caption
    If Val(Text1.Text) - Val(Text2.Text) = Label2.Caption Then
    Adodc1.Recordset.Fields("Bill_Paid").Value = Yes
    Else
    Adodc1.Recordset.Fields("Bill_Paid").Value = No
    End If
    Adodc1.Recordset.Update
    Else: confirm4 = MsgBox("Database was not updated", vbOKOnly, "Information")
    If confirm4 = vbOK Then Form5.Show
    End If
    End Sub

  19. #19

    Thread Starter
    Member
    Join Date
    Feb 2014
    Location
    In my own world
    Posts
    50

    Re: Help with codes!

    I cannot delete my post but I found out the problem myself! Thankyou

  20. #20
    Frenzied Member
    Join Date
    Jan 2009
    Location
    Watch Window(Shift+f9)
    Posts
    1,879

    Re: Help with codes!

    You have missed to set the variable .try the following hope it might help .
    Code:
     Private Sub Command2_Click()
    
    confirm3 = MsgBox("Are you sure you want to update data?", vbYesNo, "Confirmation")
    If confirm3 = vbYes Then
     Adodc1.Recordset.Fields("Customer_ID").Value = Form4.Label1.Caption
     Adodc1.Recordset.Fields("Month_ID").Value = Form4.Label7.Caption
     Adodc1.Recordset.Fields("Consumption").Value = Form4.Label1.Caption
     Adodc1.Recordset.Fields("Rate_ID").Value = Label1.Caption
     Adodc1.Recordset.Fields("Billing_Amount").Value = Label2.Caption
    If Val(Text1.Text) - Val(Text2.Text) = Label2.Caption Then
       set Adodc1.Recordset.Fields("Bill_Paid").Value = Yes
    Else
       set Adodc1.Recordset.Fields("Bill_Paid").Value = No
    End If
    Adodc1.Recordset.Update
    Else: confirm4 = MsgBox("Database was not updated", vbOKOnly, "Information")
    If confirm4 = vbOK Then Form5.Show
    End If
    End Sub

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