hello there, i need some help pls.how to update constant variables using textbox,checkbox.here my code
Code:
Public Sub Command1_Click()
SelectedPetrolType = CInt(update.Text)
MsgBox "Your price is now updated"
Label1.Caption = price
End Sub
timer1 code
Code:
Public Sub Timer1_Timer()
Text1.Text = Val(Text1.Text) + 1
Text2.Text = Text1.Text
Text2.Text = Val(Text2.Text) / SelectedPetrolType
If Text1.Text = Text3.Text Then
Timer1.Enabled = False
End If
End Sub
modules1
Code:
Public Enum PetrolTypeEnum
eNone = 0
ePremium = 40
eDiesel = 50
eUnleaded = 60
End Enum
Public SelectedPetrolType As PetrolTypeEnum
Public Sub Timer1_Timer()
Text1.Text = Val(Text1.Text) + 1
Text2.Text = Text1.Text
Text2.Text = Val(Text2.Text) / price
If Text1.Text = Text3.Text Then
Timer1.Enabled = False
End If
End Sub
module1
Code:
public price as integer
Code:
Public Sub Command1_Click()
price = CInt(update.Text)
MsgBox "Your price is now updated"
Label1.Caption = price
End Sub
Code:
Private Sub Form_Load()
price = 50
Text1.Text = "00.00" ' sets the intial value to 0
Timer1.Interval = 1 ' speed
Timer1.Enabled = False
End Sub
but when I change the code i cannot update the price I want to do Is combine the code in my first post then use my old code.its posible?
In your last threeat you had 3 variables for premium, unleaded and diesel:
VB Code:
Private Sub Form_Load()
premium = 40
diesel = 50
unleaded = 60
Text1.Text = "00.00" ' sets the intial value to 0
Timer1.Interval = 1 ' speed
Timer1.Enabled = False
End Sub
You still need to update your orignal variables as the SelectedPetrolType variable was only intended to give you a generic option to use it in your algorithm in your timer code.
you could try something like this now if you don't want to update your orignal price variable anymore:
VB Code:
Public Sub Command1_Click()
Select Case SelectedPetrolType
Case PetrolTypeEnum.eDiesel
Diesel = CInt(Update.Text)
Case PetrolTypeEnum.ePremium
Premium = CInt(Update.Text)
Case PetrolTypeEnum.eUnleaded
Unleaded = CInt(Update.Text)
End Select
MsgBox "Your price is now updated"
Label1.Caption = price
End Sub
The enum was not intended to replace your other variables. you still need them off course.
Hope this will work.
Edit:
You should not take all code advise verbatim. Its a suggestion which should help you to support your code in reaching it's final goal. If you take copy verbatim from here into your application you might even introduce more issues as we do not know the complete code structure or variable declerations of your application.
Be careful how you use or apply code samples from here and incorporate and change them to suit your application not the other way around.
Last edited by Optional; Jan 21st, 2010 at 09:40 AM.
Reason: added clarifications
Kind Regards,
Optional
If you feel this post has helped in answering your question please return the favour and Rate this post.
If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.
sorry for not explaining deeply,I want to do is update price of each command button diesel,unleaded,premium using form4,there are looping there is not working,pls help me
timer1 code
Code:
Public Sub Timer1_Timer()
Text1.Text = Val(Text1.Text) + 1
Text2.Text = Text1.Text
Text2.Text = Val(Text2.Text) / SelectedPetrolType
If Text1.Text = Text3.Text Then
Timer1.Enabled = False
End If
End Sub
my 3 command button diesel,premium,unleaded
Code:
Private Sub uiDiesel_Click()
SelectedPetrolType = eDiesel
Timer1.Enabled = True
End Sub
Private Sub uiPremium_Click()
SelectedPetrolType = ePremium
Timer1.Enabled = True
End Sub
Private Sub uiUnleaded_Click()
SelectedPetrolType = eUnleaded
Timer1.Enabled = True
End Sub
module1
Code:
Public Enum PetrolTypeEnum
eNone = 0
ePremium = 40
eDiesel = 50
eUnleaded = 60
End Enum
Public SelectedPetrolType As PetrolTypeEnum
form4-updating price
Code:
Public Sub Command1_Click()
If Option1.Value = True Then
SelectedPetrolType = CInt(update.Text)
ElseIf Option1.Value = True Then
SelectedPetrolType = CInt(update.Text)
ElseIf Option1.Value = True Then
SelectedPetrolType = CInt(update.Text)
End If
MsgBox "Your price is now updated"
Label1.Caption = price
Can you add more content to what it is that is not working ?
Does the code compile, Yes/No ?
If it does compile and it runs, which line does the code break ?
If the code doesn't break but numbers or results on screen don't look right, which values are that and what was your expected result ?
It's very hard to troubleshoot without some content around what you think is not working.
Kind Regards,
Optional
If you feel this post has helped in answering your question please return the favour and Rate this post.
If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.
optional ,thanks for code its not working maybe there something wrong in my code,ALso It have 3 checkbox for diesel,premium,unleaded .
if i choose diesel it will update only diesel ,thank for hwlping I really appreciate it
I hope you didn't copy my code in to your application verbatim as my commandbutton names were different to yours
In general what I think your app needs is:
Something like the enum for example to allow you to differentiate between what type of petrol you have selected
A variable which will store the current price of the selected type of petrol, which is also then the same variable you write the new price of petrol to
Means of saving and re-calling updated values to the a appropriate petrol types.
The implementation of all this has to be done by you off course.
I'm going to have a look at your attached code later tonight as I need to do things now at home for a few hours.
Kind Regards,
Optional
If you feel this post has helped in answering your question please return the favour and Rate this post.
If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.
You only packaged your project file.
You are missing all the forms and the module.
Kind Regards,
Optional
If you feel this post has helped in answering your question please return the favour and Rate this post.
If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.
Several mix-ups with variables etc..but no big deal.
As I previously said, the Enum should not have replaced your original Diesel, Premium and Unleaded variables. It's intended to identifiy which type of petrol you have selected.
Your Module should look like this (Module1):
VB Code:
Public Premium As Double
Public Diesel As Double
Public Unleaded As Double
Public SelectedPetrolPrice As Double
Public Enum PetrolTypeEnum
eNone = 0
ePremium = 1
eDiesel = 2
eUnleaded = 3
End Enum
Public SelectedPetrolType As PetrolTypeEnum
I changed the values in the enum to 0-3 as that's all they need to be as your actual petrol prices are still saved in the original variables. I declared them as doubles because I'm sure at some stage you going to enable decimal values I'm sure.
I added the SelectedPetrolPrice variable to allow you to store the Price of the current selected petrol type. This will allow you to generically use it in the timer code of yours.
Your command buttons on Form1 should look like this now:
VB Code:
Private Sub uiDiesel_Click()
SelectedPetrolType = eDiesel
SelectedPetrolPrice = Diesel
Timer1.Enabled = True
End Sub
Private Sub uiPremium_Click()
SelectedPetrolType = ePremium
SelectedPetrolPrice = Premium
Timer1.Enabled = True
End Sub
Private Sub uiUnleaded_Click()
SelectedPetrolType = eUnleaded
SelectedPetrolPrice = Unleaded
Timer1.Enabled = True
End Sub
Note I set the petrol type as required (eDiesel, ePremium,etc...) but I also set the Price of the petrol type you selected and assign it to the SelectedPetrolPrice variable.
Note I'm now correclty using the SelectedPetrolPrice.
When updating the petrol price on form4 your code can stay the same like this:
VB Code:
Public Sub Command1_Click()
Select Case SelectedPetrolType
Case PetrolTypeEnum.eDiesel
Diesel = CInt(update.Text)
Case PetrolTypeEnum.ePremium
Premium = CInt(update.Text)
Case PetrolTypeEnum.eUnleaded
Unleaded = CInt(update.Text)
End Select
MsgBox "Your price is now updated"
Label1.Caption = price
End Sub
(You might want to revisit, replacing cInt with something else when you want to use decimals)
This will make you application work and allow you to change price.
I did notice a lot of logical issues in your code. You are going to have to manuall fix and reset your values after changing petrol price if you want to run it straight away again without a crash.
Your code currently is heavily prone to crash due to the lack of business logic as well as input validation to guide the user into using the application as intended.
Also using Option Explicit in your code will prevent a lot of issues you had with variables not being declared but used.
This however I let you work on and figure out
I'm sure there is lots of tips and hints in the VB FAQ section regarding best practices. Definetly read up on Option Explicit.
Just to stress again, you should never rely on any code from a forum to just work in your applicatiion by simply copy-paste, it might but do not rely on it as the coder does not know your application.
Always use code from forums as a good example and shape it to your needs and not the other way around.
Otherwise, I hope you get your application into better shape now
Last edited by Optional; Jan 22nd, 2010 at 08:06 AM.
Reason: fixed some spelling and phrasing.
Kind Regards,
Optional
If you feel this post has helped in answering your question please return the favour and Rate this post.
If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.
If you feel this post has helped in answering your question please return the favour and Rate this post.
If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.
Optional, you may well know this but you don't need to specify the values in the enum if you want them to start at zero and be sequential after that. So instead of
Code:
Public Enum PetrolTypeEnum
eNone = 0
ePremium = 1
eDiesel = 2
eUnleaded = 3
End Enum
you could do
Code:
Public Enum PetrolTypeEnum
eNone
ePremium
eDiesel
eUnleaded
End Enum