Results 1 to 18 of 18

Thread: [RESOLVED] how to update constant variables

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    81

    Resolved [RESOLVED] how to update constant variables

    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
    thank you

  2. #2

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    81

    Re: how to update constant variables

    my old code is like this
    Code:
    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?

  4. #4

  5. #5
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: how to update constant variables

    Hi JD,

    In your last threeat you had 3 variables for premium, unleaded and diesel:
    VB Code:
    1. Private Sub Form_Load()
    2. premium = 40
    3. diesel = 50
    4. unleaded = 60
    5. Text1.Text = "00.00"        ' sets the intial value to 0
    6. Timer1.Interval = 1    ' speed
    7. Timer1.Enabled = False
    8. 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:
    1. Public Sub Command1_Click()
    2.     Select Case SelectedPetrolType
    3.     Case PetrolTypeEnum.eDiesel
    4.         Diesel = CInt(Update.Text)
    5.     Case PetrolTypeEnum.ePremium
    6.         Premium = CInt(Update.Text)
    7.     Case PetrolTypeEnum.eUnleaded
    8.         Unleaded = CInt(Update.Text)
    9.     End Select
    10.    
    11.     MsgBox "Your price is now updated"
    12.    
    13.     Label1.Caption = price
    14. 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.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    81

    Re: how to update constant variables

    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

  7. #7
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: how to update constant variables

    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.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    81

    Re: how to update constant variables

    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    81

    Re: how to update constant variables

    here my application
    Attached Files Attached Files

  10. #10
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: how to update constant variables

    Quote Originally Posted by jdripper31 View Post
    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.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    81

    Re: how to update constant variables

    i copy it^_^,it 11 pm here,i sleepy now,thank Optional

  12. #12
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: how to update constant variables

    Quote Originally Posted by jdripper31 View Post
    here my application
    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.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  13. #13

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    81

    Re: how to update constant variables

    sorry forgot to load form,i reupload
    Attached Files Attached Files

  14. #14
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: how to update constant variables

    I finally seen what your issue was.

    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:
    1. Public Premium As Double
    2. Public Diesel As Double
    3. Public Unleaded As Double
    4. Public SelectedPetrolPrice As Double
    5.  
    6. Public Enum PetrolTypeEnum
    7.     eNone = 0
    8.     ePremium = 1
    9.     eDiesel = 2
    10.     eUnleaded = 3
    11. End Enum
    12.  
    13. 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:
    1. Private Sub uiDiesel_Click()
    2.     SelectedPetrolType = eDiesel
    3.     SelectedPetrolPrice = Diesel
    4.     Timer1.Enabled = True
    5. End Sub
    6.  
    7. Private Sub uiPremium_Click()
    8.     SelectedPetrolType = ePremium
    9.     SelectedPetrolPrice = Premium
    10.     Timer1.Enabled = True
    11. End Sub
    12.  
    13. Private Sub uiUnleaded_Click()
    14.     SelectedPetrolType = eUnleaded
    15.     SelectedPetrolPrice = Unleaded
    16.     Timer1.Enabled = True
    17. 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.

    Your timer should look like this:
    VB Code:
    1. Public Sub Timer1_Timer()
    2.     Text1.Text = Val(Text1.Text) + 1
    3.     Text2.Text = Text1.Text
    4.     Text2.Text = Val(Text2.Text) / SelectedPetrolPrice
    5.    
    6.     If Text1.Text = Text3.Text Then
    7.         Timer1.Enabled = False
    8.     End If
    9. End Sub

    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:
    1. Public Sub Command1_Click()
    2.   Select Case SelectedPetrolType
    3.     Case PetrolTypeEnum.eDiesel
    4.         Diesel = CInt(update.Text)
    5.     Case PetrolTypeEnum.ePremium
    6.         Premium = CInt(update.Text)
    7.     Case PetrolTypeEnum.eUnleaded
    8.         Unleaded = CInt(update.Text)
    9.     End Select
    10.  
    11.     MsgBox "Your price is now updated"
    12.    
    13.     Label1.Caption = price
    14. 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.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  15. #15

    Thread Starter
    Lively Member
    Join Date
    Jan 2010
    Posts
    81

    Re: how to update constant variables

    thank you fo helping me Optional,thank for the code now I have better understanding,yes you are correct,dont depend me in any code in forum,

    Its better to educate my self,try to understand what application,how its work
    for future works i am doing since i am currently comsci student

  16. #16
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    Re: how to update constant variables

    Quote Originally Posted by jdripper31 View Post
    thank you fo helping me Optional,thank for the code now I have better understanding,yes you are correct,dont depend me in any code in forum,

    Its better to educate my self,try to understand what application,how its work
    for future works i am doing since i am currently comsci student
    No problem, I'm just glad it worked now for you. Makes me feel good

    If you need a hand with some of the things or have some general questions, feel free to send me a private message about it if you like.

    Still read the FAQ though
    http://www.vbforums.com/showthread.php?t=348141

    I will do my best to help you out.



    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.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



  17. #17
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: [RESOLVED] how to update constant variables

    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

  18. #18
    Frenzied Member
    Join Date
    Mar 2009
    Posts
    1,182

    Re: [RESOLVED] how to update constant variables

    But it is so much nicer to have those values listed...
    Option Explicit should not be an Option!

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