Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,943 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,413 people online right now. Registration is fast and FREE... Join Now!




Help on basic stuff (Newbie)

 
Reply to this topicStart new topic

Help on basic stuff (Newbie)

rjbzzr400
11 Dec, 2007 - 06:49 AM
Post #1

New D.I.C Head
*

Joined: 11 Dec, 2007
Posts: 15


My Contributions
Hi,
I've just started doing VB .net(my first programming language) at Uni.
I've got to create a basic ATM program with a login (with 4 pincodes set) which displays
the current balance (which is set in the coding) and you can deposit and withdraw money.
The changes to balance have to be saved while the program is running(I dont know if it does this automatically?)
and an admin account can view the total assets of all four users.
I'm not asking for complete code just a kick in the right direction. I've done the login screen and when you put your pin in it goes to the ATM screen but what I'm having trouble with is getting the pincode variable that has been entered (1111 or 2222 etc) onto the ATM so that I can use the set of variables I've set for each user(see second set of code) and when money is withdrawn/added saving that change in the program.

So basically
enter pin
load atm form (I dont know how to get the atm form to recognise the pin as a set variable from the previous form)
if pin = 1111 then
use user1111 variables

Also the txtpin.focus() doesn't focus the cursor in the box.

I'm a total newbie, I understand how things can work in VB I just don't know correct the syntax.
Let me know if I need to make things more clear.

If someone could guide me on how to do the things I've described it will be greatly appreciated

(Code Below vvvvvvvvvv)





CODE

     Dim pincode As String
        pincode = CStr(txtpin.Text)


        txtpin.Focus()
        If pincode = ("1111") Or pincode = ("2222") Or pincode = ("3333") Or pincode = ("4444") Then
            MessageBox.Show("Press Ok to Continue", "Pin Accepted")
            ATMScreen.Show()
        ElseIf txtpin.Text = "" Then
            MessageBox.Show("Please Enter your Pin")
        Else
            MessageBox.Show("Incorrect Pin", "Error")
            txtpin.Clear()
        End If


This code is in a different form (ATMform) and it doesn't recognise pincode from the previous form.
CODE

Dim pinCode As String
        Dim CurrentBalance As Double
        Dim OverDraft As Double

        If pinCode = ("1111") Then
            CurrentBalance = 100.27
        ElseIf pinCode = ("2222") Then
            CurrentBalance = 53.21
        ElseIf pinCode = ("3333") Then
            CurrentBalance = -48.32
            OverDraft = 100
        ElseIf pinCode = ("4444") Then
            CurrentBalance = 10.2
            OverDraft = 500
End If




User is offlineProfile CardPM
+Quote Post

SilentCodingOne
RE: Help On Basic Stuff (Newbie)
11 Dec, 2007 - 08:04 AM
Post #2

D.I.C Head
**

Joined: 8 Nov, 2007
Posts: 63


My Contributions
QUOTE(rjbzzr400 @ 11 Dec, 2007 - 07:49 AM) *

Hi,
I've just started doing VB .net(my first programming language) at Uni.
I've got to create a basic ATM program with a login (with 4 pincodes set) which displays
the current balance (which is set in the coding) and you can deposit and withdraw money.
The changes to balance have to be saved while the program is running(I dont know if it does this automatically?)
and an admin account can view the total assets of all four users.
I'm not asking for complete code just a kick in the right direction. I've done the login screen and when you put your pin in it goes to the ATM screen but what I'm having trouble with is getting the pincode variable that has been entered (1111 or 2222 etc) onto the ATM so that I can use the set of variables I've set for each user(see second set of code) and when money is withdrawn/added saving that change in the program.

So basically
enter pin
load atm form (I dont know how to get the atm form to recognise the pin as a set variable from the previous form)
if pin = 1111 then
use user1111 variables

Also the txtpin.focus() doesn't focus the cursor in the box.

I'm a total newbie, I understand how things can work in VB I just don't know correct the syntax.
Let me know if I need to make things more clear.

If someone could guide me on how to do the things I've described it will be greatly appreciated

(Code Below vvvvvvvvvv)





CODE

     Dim pincode As String
        pincode = CStr(txtpin.Text)


        txtpin.Focus()
        If pincode = ("1111") Or pincode = ("2222") Or pincode = ("3333") Or pincode = ("4444") Then
            MessageBox.Show("Press Ok to Continue", "Pin Accepted")
            ATMScreen.Show()
        ElseIf txtpin.Text = "" Then
            MessageBox.Show("Please Enter your Pin")
        Else
            MessageBox.Show("Incorrect Pin", "Error")
            txtpin.Clear()
        End If


This code is in a different form (ATMform) and it doesn't recognise pincode from the previous form.
CODE

Dim pinCode As String
        Dim CurrentBalance As Double
        Dim OverDraft As Double

        If pinCode = ("1111") Then
            CurrentBalance = 100.27
        ElseIf pinCode = ("2222") Then
            CurrentBalance = 53.21
        ElseIf pinCode = ("3333") Then
            CurrentBalance = -48.32
            OverDraft = 100
        ElseIf pinCode = ("4444") Then
            CurrentBalance = 10.2
            OverDraft = 500
End If



In order to make pinCode accessible from both you will want to make it public shared on the first form. You would put this before the beginning of your class:

Public Shared pinCode As Integer

Than you can call it by writing formName.pinCode = yada yada where formName is the actual name of the first form. You can also import the first form into the second and it will let you just write pinCode than. In regards to saving the new balance you would probably want to complete the calculation and than write your code for saving the data out to the sequential file, database or whatever else you are using to save the data to which would also be the same place you can have the program originally pull the initial user data from for each pin number.
User is offlineProfile CardPM
+Quote Post

Sothrie
RE: Help On Basic Stuff (Newbie)
11 Dec, 2007 - 08:16 AM
Post #3

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 25


My Contributions
Well the way I generally pass variables between forms is to delcare a Public variable, which can be referenced by other forms.
So in your login form, where you know what the pin code is, you'd have a line of code to pass to the other form:


CODE

     Dim pincode As String
        pincode = CStr(txtpin.Text)
        My.Forms.frmYourFormName.enteredPinCode = pincode



And in your other form I would declare a public variable to pass to. Put this somewhere near the top, outside of any events.

CODE

Public Class frmYourFormName

     Public enteredPinCode As Integer

Then you can reference this enteredPinCode where you had pinCode and it will have the value entered from the earlier form.

Also, to select a textbox, use the select command instead of the focus command: txtpin.Select()

Hope this helps!
User is offlineProfile CardPM
+Quote Post

rjbzzr400
RE: Help On Basic Stuff (Newbie)
11 Dec, 2007 - 09:20 AM
Post #4

New D.I.C Head
*

Joined: 11 Dec, 2007
Posts: 15


My Contributions
Hi, thanks alot for the help biggrin.gif
I've tried Sothrie's suggested code but it comes up with an error saying
"'enteredPin' is not a member of 'ATM.moneyscreen'" (ATM = name of project, moneyScreen = name of 2nd form)


CODE

Dim pincode As String
        pincode = CStr(txtpin.Text)
        My.Forms.moneyscreen.enteredPin = pincode


This is on moneyScreen
CODE

Public Class frmloginScreen
    Public enteredPin As String
End Class
Public Class moneyscreen
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim currentBalance As Double
        Dim overDraft As Double
        Dim enteredPin As String

        If enteredPin = ("1111") Then
            currentBalance = 100.27
        ElseIf enteredPin = ("2222") Then
            currentBalance = 53.21
        ElseIf enteredPin = ("3333") Then
            currentBalance = -48.32
            overDraft = 100
        Else : enteredPin = ("4444")
            currentBalance = 10.2
            overDraft = 500
        End If
    End Sub
End Class



And how would I go about putting in/starting code thats not from a button click, for instance once the pin has been selected I need to
select that pin's associated variables (Like the 2nd code I've shown above)

Thanks : >


User is offlineProfile CardPM
+Quote Post

Sothrie
RE: Help On Basic Stuff (Newbie)
11 Dec, 2007 - 11:48 AM
Post #5

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 25


My Contributions
Haha, miscommunication on my part! Your public variable enteredPin doesn't have its' own class, it is just part of the class that makes up the form. Put moneyScreen like this instead:

CODE


Public Class moneyscreen
    Public enteredPin As String '<=Notice this is in the form's actual class
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim currentBalance As Double
        Dim overDraft As Double
        Dim enteredPin As String

        If enteredPin = ("1111") Then
            currentBalance = 100.27
        ElseIf enteredPin = ("2222") Then
            currentBalance = 53.21
        ElseIf enteredPin = ("3333") Then
            currentBalance = -48.32
            overDraft = 100
        Else : enteredPin = ("4444")
            currentBalance = 10.2
            overDraft = 500
        End If
    End Sub
End Class


As for starting code not in a button click, you would just have to find the event that most closely corresponds with what you want to do. If you select an object in the designer and view its properties, there is a lightning-bolt-looking button that will show you all of the events related to a control.
So for instance, if you wanted to do something with a text box's entered text after the user moves away from it, you would create a TextBox.Leave event.

Hope that's not too ambiguous or too simple!
User is offlineProfile CardPM
+Quote Post

rjbzzr400
RE: Help On Basic Stuff (Newbie)
12 Dec, 2007 - 02:15 AM
Post #6

New D.I.C Head
*

Joined: 11 Dec, 2007
Posts: 15


My Contributions
Hi, thanks hehe. I've got another problem I'm trying to write enteredMoney + currentBalance as below, but it defaults to enteredMoney(+currentBalance) and underlines enteredMoney in blue and says "Expression is not a method" and I've got no idea why.
Sorry to keep asking questions. I've got programing class tomorrow so I'll get some help then but I'm trying to get as much done as I can : >
CODE


Public Class moneyscreen
    Public enteredPin As String
    Public currentBalance As Double
    Public overDraft As Double



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        If enteredPin = ("1111") Then
            currentBalance = 100.27
            overDraft = 0
        ElseIf enteredPin = ("2222") Then
            currentBalance = 53.21
            overDraft = 0
        ElseIf enteredPin = ("3333") Then
            currentBalance = -48.32
            overDraft = 100
        Else : enteredPin = ("4444")
            currentBalance = 10.2
            overDraft = 500
        End If
    End Sub

    Private Sub btnDeposit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeposit.Click
        Dim enteredMoney As Double
        enteredMoney = CDbl(txtMoney.Text)

        enteredMoney(+currentBalance)

    End Sub

End Class

User is offlineProfile CardPM
+Quote Post

Sothrie
RE: Help On Basic Stuff (Newbie)
12 Dec, 2007 - 06:05 AM
Post #7

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 25


My Contributions
Cool. Easy fix. VB is assuming that enteredMoney is a function because there's no equal sign on the line. You're not setting this addition equal to anything. So if you wanted to, for example, add currentBalance to enteredMoney you'd do this:
CODE

enteredMoney = enteredMoney + currentBalance


No worries about asking questions. That's what the forums are for! smile.gif
User is offlineProfile CardPM
+Quote Post

rjbzzr400
RE: Help On Basic Stuff (Newbie)
14 Dec, 2007 - 12:25 PM
Post #8

New D.I.C Head
*

Joined: 11 Dec, 2007
Posts: 15


My Contributions
Hi, thanks alot for your help almost finished : >
I have to make a boss account that displays the total assets of the 4 accounts, I've
got the boss account working but it doesnt update the total assets when a change is made
to the accounts.


CODE


Public Class Form1

    Private enteredPin As String
    Private currentBalance1 As Double = 100.27
    Private currentBalance2 As Double = 53.21
    Private currentBalance3 As Double = -48.32
    Private currentBalance4 As Double = 10.2
    Private totalAssets As Double = currentBalance1 + currentBalance2 + _
    currentBalance3 + currentBalance4

    Private overDraft1 As Double = 0
    Private overDraft2 As Double = 0
    Private overDraft3 As Double = 100
    Private overDraft4 As Double = 500

    Private enteredMoney As Double
    Private transaction As Double




    Private Sub btnpin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpin.Click
        Dim pincode As String
        pincode = CStr(txtpin.Text)
        enteredPin = pincode



        txtpin.Select()
        If pincode = ("1111") Or pincode = ("2222") Or pincode = ("3333") _
        Or pincode = ("4444") Or pincode = ("boss") Then
            MessageBox.Show("Press Ok to Continue", "Pin Accepted")
            loginBox.Hide()
            atmBox.Show()
            atmBox.Refresh()
        ElseIf txtpin.Text = "" Then
            MessageBox.Show("Please Enter your Pin, Error")
        Else
            MessageBox.Show("Incorrect Pin", "Error")
            txtpin.Clear()
        End If

        Refresh()
        txtMoney.Clear()

        If enteredPin = ("1111") Then
            txtBalance.Text = CStr(currentBalance1)
            txtOverdraft.Text = CStr(overDraft1)
        ElseIf enteredPin = ("2222") Then
            txtBalance.Text = CStr(currentBalance2)
            txtOverdraft.Text = CStr(overDraft2)
        ElseIf enteredPin = ("3333") Then
            txtBalance.Text = CStr(currentBalance3)
            txtOverdraft.Text = CStr(overDraft3)
        ElseIf enteredPin = ("4444") Then
            txtBalance.Text = CStr(currentBalance4)
            txtOverdraft.Text = CStr(overDraft4)
        ElseIf enteredPin = ("boss") Then
            Refresh()
            atmBox.Hide()
            bossBox.Show()
            txtTotalAssets.Text = CStr(totalAssets)
        End If

        txtMoney.Select()





    End Sub

    Private Sub btnDeposit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeposit.Click

        enteredMoney = CDbl(txtMoney.Text)


        If enteredPin = ("1111") Then
            currentBalance1 = enteredMoney + currentBalance1
            txtBalance.Text = CStr(currentBalance1)
        ElseIf enteredPin = ("2222") Then
            currentBalance2 = enteredMoney + currentBalance2
            txtBalance.Text = CStr(currentBalance2)
        ElseIf enteredPin = ("3333") Then
            currentBalance3 = enteredMoney + currentBalance3
            txtBalance.Text = CStr(currentBalance3)
        Else : enteredPin = ("4444")
            currentBalance4 = enteredMoney + currentBalance4
            txtBalance.Text = CStr(currentBalance4)
        End If
    End Sub

    Private Sub btnWithdraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWithdraw.Click
        enteredMoney = CDbl(txtMoney.Text)


        If enteredPin = ("1111") Then
            If overDraft1 = 0 And currentBalance1 = 0 Then
                MessageBox.Show("No funds available", "WARNING")
            End If

            If overDraft1 > 0 And enteredMoney > overDraft1 Then
                MessageBox.Show("Insufficient overdraft available", "WARNING")
            End If

            If enteredMoney > currentBalance1 And overDraft1 > 0 Then
                transaction = currentBalance1 - enteredMoney
                currentBalance1 = 0
                overDraft1 = overDraft1 + transaction
                txtOverdraft.Text = CStr(overDraft1)
                txtBalance.Text = CStr(currentBalance1)

            ElseIf enteredMoney > currentBalance1 Then
                MessageBox.Show("Insufficient funds available")
            Else
                currentBalance1 = currentBalance1 - enteredMoney
                txtBalance.Text = CStr(currentBalance1)
            End If







        ElseIf enteredPin = ("2222") Then
            If overDraft2 = 0 And currentBalance2 = 0 Then
                MessageBox.Show("No funds available", "WARNING")
            End If

            If overDraft2 > 0 And enteredMoney > overDraft2 Then
                MessageBox.Show("Insufficient overdraft available", "WARNING")
            End If
            If enteredMoney > currentBalance2 And overDraft2 > 0 Then
                transaction = currentBalance2 - enteredMoney
                currentBalance2 = 0
                overDraft2 = overDraft2 + transaction
                txtOverdraft.Text = CStr(overDraft2)
                txtBalance.Text = CStr(currentBalance2)
            ElseIf enteredMoney > currentBalance2 Then
                MessageBox.Show("Insufficient funds available")
            Else
                currentBalance2 = currentBalance2 - enteredMoney
                txtBalance.Text = CStr(currentBalance2)
            End If



        ElseIf enteredPin = ("3333") Then
            If overDraft3 = 0 And currentBalance3 = 0 Then
                MessageBox.Show("No funds available", "WARNING")
            End If
            If overDraft3 > 0 And enteredMoney > overDraft3 Then
                MessageBox.Show("Insufficient overdraft available", "WARNING")
            End If
            If enteredMoney > currentBalance3 And overDraft3 > 0 Then
                transaction = currentBalance3 - enteredMoney
                currentBalance3 = 0
                overDraft3 = overDraft3 + transaction
                txtOverdraft.Text = CStr(overDraft3)
                txtBalance.Text = CStr(currentBalance3)
            ElseIf enteredMoney > currentBalance3 Then
                MessageBox.Show("Insufficient funds available")
            Else
                currentBalance3 = currentBalance3 - enteredMoney
                txtBalance.Text = CStr(currentBalance3)
            End If




        Else : enteredPin = ("4444")
            If overDraft4 = 0 And currentBalance4 = 0 Then
                MessageBox.Show("No funds available", "WARNING")
            End If
            If overDraft4 > 0 And enteredMoney > overDraft4 Then
                MessageBox.Show("Insufficient overdraft available", "WARNING")
            End If
            If enteredMoney > currentBalance4 And overDraft4 > 0 Then
                transaction = currentBalance4 - enteredMoney
                currentBalance4 = 0
                overDraft4 = overDraft4 + transaction
                txtOverdraft.Text = CStr(overDraft4)
                txtBalance.Text = CStr(currentBalance4)
            ElseIf enteredMoney > currentBalance4 Then
                MessageBox.Show("Insufficient funds available", "WARNING")
            Else
                currentBalance4 = currentBalance4 - enteredMoney
                txtBalance.Text = CStr(currentBalance4)
            End If
        End If



    End Sub

    Private Sub Form1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
        Refresh()
        txtMoney.Clear()
    End Sub

    Private Sub btnLogOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogOff.Click
        atmBox.Hide()
        loginBox.Show()
        txtpin.Clear()
        txtpin.Select()
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtpin.Select()

    End Sub
End Class



User is offlineProfile CardPM
+Quote Post

Sothrie
RE: Help On Basic Stuff (Newbie)
16 Dec, 2007 - 08:29 AM
Post #9

New D.I.C Head
*

Joined: 3 Dec, 2007
Posts: 25


My Contributions
Well that's because you don't update the total assets when a change is made. What I would do is just retotal it whenever a deposit or withdraw is made
CODE

    Private Sub btnDeposit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeposit.Click

        enteredMoney = CDbl(txtMoney.Text)


        If enteredPin = ("1111") Then
            currentBalance1 = enteredMoney + currentBalance1
            txtBalance.Text = CStr(currentBalance1)
        ElseIf enteredPin = ("2222") Then
            currentBalance2 = enteredMoney + currentBalance2
            txtBalance.Text = CStr(currentBalance2)
        ElseIf enteredPin = ("3333") Then
            currentBalance3 = enteredMoney + currentBalance3
            txtBalance.Text = CStr(currentBalance3)
        Else : enteredPin = ("4444")
            currentBalance4 = enteredMoney + currentBalance4
            txtBalance.Text = CStr(currentBalance4)
        End If

        'Resum the totalAssets
    totalAssets = currentBalance1 + currentBalance2 + _
    currentBalance3 + currentBalance4

    End Sub


Private Sub btnWithdraw_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWithdraw.Click
        enteredMoney = CDbl(txtMoney.Text)


        If enteredPin = ("1111") Then
            If overDraft1 = 0 And currentBalance1 = 0 Then
                MessageBox.Show("No funds available", "WARNING")
            End If

            If overDraft1 > 0 And enteredMoney > overDraft1 Then
                MessageBox.Show("Insufficient overdraft available", "WARNING")
            End If

            If enteredMoney > currentBalance1 And overDraft1 > 0 Then
                transaction = currentBalance1 - enteredMoney
                currentBalance1 = 0
                overDraft1 = overDraft1 + transaction
                txtOverdraft.Text = CStr(overDraft1)
                txtBalance.Text = CStr(currentBalance1)

            ElseIf enteredMoney > currentBalance1 Then
                MessageBox.Show("Insufficient funds available")
            Else
                currentBalance1 = currentBalance1 - enteredMoney
                txtBalance.Text = CStr(currentBalance1)
            End If


            [...]
        Else : enteredPin = ("4444")
            If overDraft4 = 0 And currentBalance4 = 0 Then
                MessageBox.Show("No funds available", "WARNING")
            End If
            If overDraft4 > 0 And enteredMoney > overDraft4 Then
                MessageBox.Show("Insufficient overdraft available", "WARNING")
            End If
            If enteredMoney > currentBalance4 And overDraft4 > 0 Then
                transaction = currentBalance4 - enteredMoney
                currentBalance4 = 0
                overDraft4 = overDraft4 + transaction
                txtOverdraft.Text = CStr(overDraft4)
                txtBalance.Text = CStr(currentBalance4)
            ElseIf enteredMoney > currentBalance4 Then
                MessageBox.Show("Insufficient funds available", "WARNING")
            Else
                currentBalance4 = currentBalance4 - enteredMoney
                txtBalance.Text = CStr(currentBalance4)
            End If
        End If

        'Resum the totalAssets
    totalAssets = currentBalance1 + currentBalance2 + _
    currentBalance3 + currentBalance4

    End Sub


And then when you reference it, the total should be updated.
Of course, the totals for the accounts and the total won't persist when you close and restart your project. But that might not be a problem, depending on what your program is required to do.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:53PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month