Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Visual basic 6.0 Looping help

  • 09-12-2008 2:29pm
    #1
    Registered Users, Registered Users 2 Posts: 3,627 ✭✭✭


    Ok im still a beginner in vb programming but would any of ye be able to help me out with this.Lets say you have 3 bar deals:1 bar = 7e,2bars = 8e,3 bars = 9e.Thats the grouped discount so for example if he wanted 5 bars he pays for them at a rate of (3 bars = 9 And 2 bars =8) as opposed to (5 * 1 bar = 7).
    Secondly they will type a value in a quantity textbox as to how many bars they want.Anyway i would like to make a loop to enable this discount so could anyone please help.

    Summary:
    1 bar = 7e,2 bars = 8e, 3 bars = 9e
    They type the amount they want into a quantity box

    id Like the loop to kinda look like this,if it makes any sense;
    if quantity >= 3 then
    do Price = (quantity - 3)
    loop until quantity < 3
    if quantity < 3 then
    if quantity = 2 then
    price = price + 8
    endif
    if quantity = 1 then
    price = price + 7

    for every loop done ad 9 to the Price




    Im sorry if i use bad programming practice but as i said im only learning.
    Any help much appreciated,cheers.


Comments

  • Registered Users, Registered Users 2 Posts: 349 ✭✭ecaf


    I would use a divider and a modulus
    (probably not great code, but it should work).

    e.g.
    dim quantity as integer, multiplier as integer, remainder as integer
    
    if quantity >= 3 then
      multiplier = quantity/3
      price = multiplier x 9
    
      remainder = quantity mod 3
    
        if remainder = 1 then
            price = price + 7
        elseif remainder = 2 then
            price = price + 8
        end if
    
    elseif quantity = 1 then
      price = 7
    elseif quantity = 2 then
      price = 8
    end if
    


  • Closed Accounts Posts: 3,357 ✭✭✭Beano


    for the problem as expressed you dont need a loop. its only when you expand the problem into more than 3 units/price variants that you would need a loop. lookup the integer division ( \ ) and mod operators.


  • Registered Users, Registered Users 2 Posts: 3,627 ✭✭✭Fol20


    ecaf wrote: »
    I would use a divider and a modulus
    (probably not great code, but it should work).

    e.g.
    dim quantity as integer, multiplier as integer, remainder as integer
    
    if quantity >= 3 then
      multiplier = quantity/3
      price = multiplier x 9
    
      remainder = quantity mod 3
    
        if remainder = 1 then
            price = price + 7
        elseif remainder = 2 then
            price = price + 8
        end if
    
    elseif quantity = 1 then
      price = 7
    elseif quantity = 2 then
      price = 8
    end if
    

    i dont think that will work...lets say you have 4 how will it divide..it wont be an integer then like unless "remainder = quantity mod 3" makes that possible.What does that sentence do for you and also the reserved word mod..i havent come across them yet


    I tried your code basically..and i couldnt get it working.It kept just giving 0
    I had a lblResult,txtQuantity and cmdCalculate
    take a look at the pic to see what i mean


  • Registered Users, Registered Users 2 Posts: 349 ✭✭ecaf


    You forgot to set quantity = txtQuantity.text

    I have tried it and it works if quantity = 4 then Price = 16
    Put a break point and step through the code, you really need to try and understand this code, especially if you are going to use it for college.

    Edit: You should also set all variables to 0 at the start.

    Edit 2: Sorry but I typed in the wrong operator in the sample code it should be \ not / --> This does work.


  • Registered Users, Registered Users 2 Posts: 3,627 ✭✭✭Fol20


    Yea thanks a lot it works now..btw i do want to understand it but as i said i am still learning how programming properly works..
    i was wondering could you outline a few things for me.

    1) what does this do/mean "quantity mod 3"
    2)what does "\" do opposed to "/"
    3)Actually i have 4 but what can you do:I was wondering how use use the isnumeric function properly.So far i type this:
    If IsNumeric(txtQuantity.Text) Then
            x = (1 * Val(txtQuantity.Text))
            End If
    

    I was wondering is their any other way to use it if its easier.

    4)I also have an error here with a masking box,It works fine if i type in any number,but if i leave it blank i get a run time error and it highlights this code
        If mskAge.Text < 18 Then
    

    This is the code for it
    If mskAge.Text = "___" Then
        MsgBox "Please enter your age", vbCritical, "Required"
        End If
        
        If mskAge.Text < 18 Then
        MsgBox "Only people over 18 can use our services", vbCritical, "Important"
        End If
    

    5)Finally i know i must be bugging you at this stage with so many questions but i thank you for your help ecaf,Anyway the final problem is in relation with masking boxes.I was wondering how do you force a masking box to start at the first character when someone clicks on a masking box instead of where they physically clicked in the masking box.That maybe confused so ill try and give an example:With text boxes if you click anywhere in the text box it will start on the left hand side.Maskingboxs on the other hand can vary.if you click the righthand side of it,it will go to the last character in the masking box


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 349 ✭✭ecaf


    Fol20 wrote: »
    Yea thanks a lot it works now..btw i do want to understand it but as i said i am still learning how programming properly works..
    i was wondering could you outline a few things for me.

    1) what does this do/mean "quantity mod 3"
    2)what does "\" do opposed to "/"
    3)Actually i have 4 but what can you do:I was wondering how use use the isnumeric function properly.So far i type this:
    If IsNumeric(txtQuantity.Text) Then
            mcurPricex = (1 * Val(txtQuantity.Text))
            End If
    

    I was wondering is their any other way to use it if its easier.

    4)I also have an error here with a masking box,It works fine if i type in any number,but if i leave it blank i get a run time error and it highlights this code
        If mskAge.Text < 18 Then
    

    This is the code for it
    If mskAge.Text = "___" Then
        MsgBox "Please enter your age", vbCritical, "Required"
        End If
        
        If mskAge.Text < 18 Then
        MsgBox "Only people over 18 can use our services", vbCritical, "Important"
        End If
    

    5)Finally i know i must be bugging you at this stage with so many questions but i thank you for your help ecaf,Anyway the final problem is in relation with masking boxes.I was wondering how do you force a masking box to start at the first character when someone clicks on a masking box instead of where they physically clicked in the masking box.That maybe confused so ill try and give an example:With text boxes if you click anywhere in the text box it will start on the left hand side.Maskingboxs on the other hand can vary.if you click the righthand side of it,it will go to the last character in the masking box

    1) Mod finds the remainder - so 5 mod 3 is 2 (5 divided by 3 = 1 and 2 over)
    So for your price example I take the 1 * 9 and you said that 2 = price of 8. I'm just using this instead of your loops that takes of -3 every time.

    2) / is divide 5/3 = returns 2 (rounded up). But \ is integer division and 5\3 returns 1.
    You should just simplify your program and step through it with both operators to see what it gives when you use different quantities.

    3) The isnumeric is fine, you could probably put in an else condition to warn the user that it isn't a number that is entered, or to delete the text if is is not a number. Use KeyAscii from 48 - 57 to validate a number.

    4) You probably need IsNumeric on your masked edit box

    5)Not sure but try .SelStart = 1 or google this if it doesn't work and see if you can find something similar for masked edit boxes.


  • Registered Users, Registered Users 2 Posts: 3,627 ✭✭✭Fol20


    iv decided that even though your code is working,i cant use it until i come to the chapter with that type of stuff and fully understand it,plus it would look odd if my lecture sees thats when we havnt done it.

    Can you see what im doing wrong here

    Dim intXQuantity As Integer
    Dim mcurPriceX As Currency
    
    
    If chkX.Value = 1 Then
            If IsNumeric(txtXQuantity.Text) Then
                If Val(txtXQuantity.Text) >= 3 Then
                Do Until Val(txtXQuantity.Text) < 3
                XQuantity = val(txtXQuantity.Text> - 3
                mcurPriceX = mcurPriceX + 9
                Loop
                ElseIf Val(txtXQuantity.Text) = 1 Then
                mcurPriceX = mcurPriceX + 7
                ElseIf Val(txtXQuantity.Text) = 2 Then
                mcurPriceX = mcurPriceX + 8
                Else: MsgBox "Please enter A Quantity ",  vbCritical, "Required"
                End If
            End If
        End If
    


    I think the line "XQuantity = val(txtXQuantity.Text> - 3" is vital and thats probably where im going wrong.Its meant to be +1 but then how would i subtract - 3 for everyloop then


  • Registered Users, Registered Users 2 Posts: 349 ✭✭ecaf


    You should assign txtXQuantity.Text to XQuantity before your Do ... Loop.
    Then put XQuantity into the Do Until.

    The reason it is not working is because you are setting a value to XQuantity and deducting 3 from it, but in the DO loop you are referring to txtXQuantity.text which you are not deducting anything from.

    In practice if you are taking a value from an input box you should always assign the value to a variable (i.e. XQuantity), instead of working directly with the text box value. The same should go for your ElseIf statements - you are also referring to the text box value.

    Also for clarity of reading code you should indent your code after each condition (IF / DO / FOR... etc). The rest looks fine.


  • Registered Users, Registered Users 2 Posts: 3,627 ✭✭✭Fol20


    yea i eventually got it working.Btw i did try to make txtXquantity-xquantity and i couldnt get it working that way..Im gonna indent the do and loop thing as you said..keep in mind that im still only learning and this is my first real go of vb since i started..Thanks for all your help though,youv been very informative.:)


  • Registered Users, Registered Users 2 Posts: 349 ✭✭ecaf


    No problems. That's how I learnt for college too. Its good that you can do it two different ways now, at least your not just copying down the code and using it, because that way you won't learn anything and you'll struggle more later on.
    Best of luck with your course.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 3,627 ✭✭✭Fol20


    I got one more problem,So im at the end of my project and now have to display an invoice of what they have selected..Basically i want a listbox to display whatever they chose.My problem is that im not entirely sure how to declare option boxes and checkboxs as global and secondly would this work after you declare them as globals



    If chkX.Value = vbChecked Then

    frmInvoice!lstBars.AddItem "X"

    End If

    End Sub


  • Registered Users, Registered Users 2 Posts: 349 ✭✭ecaf


    I would declare a global variable at the start of your project. Then if an check box is selected then assign a value to the variable, then use the variable to add to the list.

    It would be something like
    Public XValue as string
    

    and your check box will be
    If chkX.value = 1 then 'This means it is selected (0 means it is not selected).
    

    Your statement below is more in MS Access format:
    frmInvoice!lstBars.AddItem "X"

    it should be
    frmInvoice.lstBars.AddItem XValue
    

    You will also need to use the index value to add to the list, if you need to select from the list in the future.
    I would use i as integer for this, and add 1 to i each time you do an add item
    So it would be
    frmInvoice.lstBars.AddItem XValue, i
    i=i+1
    


  • Registered Users, Registered Users 2 Posts: 3,627 ✭✭✭Fol20


    Yea i got it working in the end.
    Public intX as integer
    
    if chkX.value = 1 then
     intX = 1
    
    'invoice
    if intX = 1 then
    lstX.additem "X"
    


Advertisement