-
Feb 12th, 2014, 02:38 PM
#1
Thread Starter
Member
[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
-
Feb 12th, 2014, 03:57 PM
#2
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)
....
-
Feb 12th, 2014, 04:01 PM
#3
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
-
Feb 12th, 2014, 04:11 PM
#4
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
-
Feb 12th, 2014, 04:20 PM
#5
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
-
Feb 12th, 2014, 04:24 PM
#6
Re: Help with codes!
@RhinoBull
Sorry, didn't see your post before posting my answer!
-
Feb 12th, 2014, 09:59 PM
#7
Re: Help with codes!
Originally Posted by SamOscarBrown
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
-
Feb 13th, 2014, 12:05 AM
#8
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
-
Feb 13th, 2014, 02:08 AM
#9
Thread Starter
Member
-
Feb 13th, 2014, 02:45 AM
#10
Re: Help with codes!
So many smileys
-
Feb 13th, 2014, 03:21 AM
#11
Thread Starter
Member
Re: Help with codes!
Is it um.. like a taboo here?..
-
Feb 13th, 2014, 04:58 AM
#12
Frenzied Member
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
-
Feb 13th, 2014, 05:36 AM
#13
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)
-
Feb 13th, 2014, 08:19 AM
#14
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!
-
Feb 13th, 2014, 08:43 AM
#15
Re: Help with codes!
Originally Posted by SamOscarBrown
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.
-
Feb 13th, 2014, 09:22 AM
#16
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.
-
Feb 13th, 2014, 09:54 AM
#17
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)
-
Feb 15th, 2014, 12:24 PM
#18
Thread Starter
Member
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
-
Feb 15th, 2014, 02:59 PM
#19
Thread Starter
Member
Re: Help with codes!
I cannot delete my post but I found out the problem myself! Thankyou
-
Feb 16th, 2014, 06:55 AM
#20
Frenzied Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|