Results 1 to 20 of 20

Thread: declare a varible so I can use it anywhere??

  1. #1

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91

    Unhappy declare a varible so I can use it anywhere??

    I have a question. I have a program that uses a common dialogue box to import a .dat file. the .dat file has lines of info like this for example:

    "905","Car, Sedan",22897
    "503","Yogurt, Plain",1.28
    "601","Cola, 2 ltr",1.29
    "602","Cola, diet 2 ltr",1.29
    "603","Cola, 6 pack",1.85

    I open the file like this:

    ProdFileName = CDL1.FileName '** Remember product file name
    Open ProdFileName For Input As #6 '** Open the product file
    I = 1 '** Start with first product
    Do Until EOF(6) '** Until the end of the file
    Input #6, UPC(I), ProdDesc(I), ProdPrice(I) '** Read I-th product
    I = I + 1 '** Anticipate next product
    Loop

    NumProducts = I - 1 '** Remember number of products read
    Close #6

    having CDL1 the name of the common dialogue box. Now when I want to call UPC(I), ProdDesc(I), or ProdPrice(I), I will have to call them in two different form. This means that I have to declare the varibles Globally. How exactly do I do this?? Do I have to use a modual? I tried this:

    Global UPC(MaxProducts) As String '** Array for UPC codes
    Global ProdDesc(MaxProducts) As String '** Product descriptions
    Global ProdPrice(MaxProducts) As Currency '** Product prices

    Global I As Integer '** Variable used to index these arrays
    Global NumProducts As Integer '** Variable providing number of products read.

    I put that into a modual, and I recieved an error when I was trying to open ProdFileName. I defined ProdFileName as Global in the modual as well.

    '------Varibles to define the arrays------------

    Global ProdFileName As String '** Variable providing name of product file.

    What am I doing wrong?? Please help me. Thanks

    Brian

  2. #2
    Hyperactive Member ZeroCool's Avatar
    Join Date
    Feb 2002
    Location
    In front of my computer
    Posts
    423
    Whats The Error?
    Visual Baisc 6 (SP5)
    Windows Xp

  3. #3

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    runtime error '62'

    input past end-of-file

    the debugger stops here:

    Input #6, UPC(I), ProdDesc(I), ProdPrice(I) '** Read I-th product

  4. #4
    Hyperactive Member ZeroCool's Avatar
    Join Date
    Feb 2002
    Location
    In front of my computer
    Posts
    423
    This:

    VB Code:
    1. Do Until EOF(6) '** Until the end of the file

    Should Be this:

    VB Code:
    1. Do Until EOF(6)  = True
    Visual Baisc 6 (SP5)
    Windows Xp

  5. #5

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    nope. That wasn't the answer. Thanks though for your help. I am still searching... I think I declared my varibles wrong. if I have an array, and I want the program to read the array, but the varibles in the array defined globally, how would I do that? (I basically want to be able to use the varibles anywhere)

    Brian
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  6. #6

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    arrg! this is bothering me..
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  7. #7

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    no one yet??
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  8. #8
    Hyperactive Member ZeroCool's Avatar
    Join Date
    Feb 2002
    Location
    In front of my computer
    Posts
    423
    Post your Project and The .dat File
    Visual Baisc 6 (SP5)
    Windows Xp

  9. #9

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    You want me to just copy and paste the code?
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  10. #10
    Dreamlax
    Guest
    Is there a carriage return or line feed at the end of the file? If there is, that might cause some problems.

  11. #11
    Hyperactive Member ZeroCool's Avatar
    Join Date
    Feb 2002
    Location
    In front of my computer
    Posts
    423
    Ya unless you dont wan to past the code on here for some reason
    Visual Baisc 6 (SP5)
    Windows Xp

  12. #12
    Frenzied Member
    Join Date
    Dec 2000
    Posts
    1,195
    add a module

    Public VARIABLE as String (or whatever)

    then you can use VARIABLE wherever

  13. #13

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    '**this is my module

    Option Explicit

    '** The following constant is the width of the receipt in characters.
    '** Note, the receipt Text Box was widened and its point size decreased
    '** to 10. If you change the width or point size of the receipt Text Box,
    '** this constant must also be changed.
    Const ReceiptWidth As Integer = 29

    Const MaxProducts As Integer = 200 ' Maximum number of Products store can carry

    Const MaxEmployee As Integer = 12 ' Maximum number of Employees in the employee file

    Const MaxTrans As Double = 100000 ' Maximum number of Transactions that the store can do

    Const TaxRate As Currency = 0.065 ' Sales tax rate

    Global AmountTendered As Currency ' Amount of money offered by custmer to pay bill
    Global SubTotal As Double ' Running subtotal of items purchased
    Global SalesTax As Double ' Sales tax due on the order
    Global AmountDue As Single ' Total amount to charge customer = SubTotal + SalesTax

    '------Variables for the Products File---------

    Global UPC(MaxProducts) As String '** Array for UPC codes
    Global ProdDesc(MaxProducts) As String '** Product descriptions
    Global ProdPrice(MaxProducts) As Currency '** Product prices

    Global I As Integer '** Variable used to index these arrays
    Global NumProducts As Integer '** Variable providing number of products read.

    '------Variables for the Employee File---------

    Global EmployeeNum(MaxEmployee) As String '** This is the Employee's Number
    Global EmployeeName(MaxEmployee) As String '** This is the Employee's Name
    Global EmployeePass(MaxEmployee) As String '** This is the Employee's Password
    Global ManagerStatus(MaxEmployee) As Boolean '** This is to determine if the Employee has Manager Status/Rights

    Global J As Integer '** Variable used to index these arrays
    Global NumEmployee As Integer '** Variable providing number of Employees Logged in.

    '------Varibles for the Transactions File-------

    Global TransDate(MaxTrans) As Date '** This is the Date when the sale was made
    Global TransEmpNum(MaxTrans) As String '** This is the Employee Number of the individual who was logged in
    Global TransUPC(MaxTrans) As String '** This is the UPC of the product sold
    Global TransPrice(MaxTrans) As Currency '** This is the price of the item sold

    Global K As Integer '** Variable used to index these arrays
    Global NumTrans As Integer '** Variable providing number of Transactions preformed.

    '------Varibles to define the arrays------------

    Global ProdFileName As String '** Variable providing name of product file.
    Global EmpFileName As String '** Variable providing name of employee file.
    Global TransFileName As String '** Variable providing name of trans file.

    Function PL(A As String, I As Integer)
    Dim Result As String

    Result = A
    Do Until Len(Result) >= I
    Result = " " & Result




    Loop
    PL = Left(Result, I)

    End Function
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  14. #14

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    and this is the form load code:

    '** Procedure added to allow definition of the input file
    '** and to read all data from it.
    Private Sub Form_Load()


    CDL1.DialogTitle = "Open the Product Data File" '** set title for the dialog box
    CDL1.FileName = "C:\WINDOWS\Desktop\hw10\hw10\sample\products.dat" '** identifies file on my pc
    CDL1.Flags = cdlOFNFileMustExist '** allow existing files only
    CDL1.Filter = "All Data Files (*.dat)|*. dat|All Files (*.*)|*. *" '** set filter of files to display
    CDL1.CancelError = True '** ShowOpen method returns error if Cancel clikced
    On Error Resume Next '** Disable VB error trapping and handling
    CDL1.ShowOpen '** Pop up ShowOPen dialog
    If Err.Number = cdlCancel Then '** If Cancel Button Clicked
    MsgBox "Cancel button clicked. Program ends." '** Inform user
    End '** And, stop the program
    Else
    If Err.Number > 0 Then
    '** All other error should be reported
    '** to programmer for correction.
    MsgBox ("Catastrophic Error. Please inform programmer. Program stops.")
    End
    End If
    End If

    Err.Clear '** Clear the error, if any.
    On Error GoTo 0 '** Have VB handle error trapping.

    CDL2.DialogTitle = "Open the Employee Data File" '** set title for the dialog box
    CDL2.FileName = "employee.dat" '** identifies file on my pc
    CDL2.Flags = cdlOFNFileMustExist '** allow existing files only
    CDL2.Filter = "All Data Files (*.dat)|*. dat|All Files (*.*)|*. *" '** set filter of files to display
    CDL2.CancelError = True '** ShowOpen method returns error if Cancel clikced
    On Error Resume Next '** Disable VB error trapping and handling
    CDL2.ShowOpen '** Pop up ShowOPen dialog
    If Err.Number = cdlCancel Then '** If Cancel Button Clicked
    MsgBox "Cancel button clicked. Program ends." '** Inform user
    End '** And, stop the program
    Else
    If Err.Number > 0 Then
    '** All other error should be reported
    '** to programmer for correction.
    MsgBox ("Catastrophic Error. Please inform programmer. Program stops.")
    End
    End If
    End If

    Err.Clear '** Clear the error, if any.
    On Error GoTo 0 '** Have VB handle error trapping.

    CDL3.DialogTitle = "Open the Transactions Data File" '** set title for the dialog box
    CDL3.FileName = "trans.dat" '** identifies file on my pc
    CDL3.Flags = cdlOFNFileMustExist '** allow existing files only
    CDL3.Filter = "All Data Files (*.dat)|*. dat|All Files (*.*)|*. *" '** set filter of files to display
    CDL3.CancelError = True '** ShowOpen method returns error if Cancel clikced
    On Error Resume Next '** Disable VB error trapping and handling
    CDL3.ShowOpen '** Pop up ShowOPen dialog
    If Err.Number = cdlCancel Then '** If Cancel Button Clicked
    MsgBox "Cancel button clicked. Program ends." '** Inform user
    End '** And, stop the program
    Else
    If Err.Number > 0 Then
    '** All other error should be reported
    '** to programmer for correction.
    MsgBox ("Catastrophic Error. Please inform programmer. Program stops.")
    End
    End If
    End If

    Err.Clear '** Clear the error, if any.
    On Error GoTo 0 '** Have VB handle error trapping.


    ProdFileName = CDL1.FileName '** Remember product file name
    Open ProdFileName For Input As #6 '** Open the product file
    I = 1 '** Start with first product
    Do Until EOF(6) '** Until the end of the file
    Input #6, UPC(I), ProdDesc(I), ProdPrice(I) '** Read I-th product
    I = I + 1 '** Anticipate next product
    Loop

    NumProducts = I - 1 '** Remember number of products read
    Close #6 '** Close the file


    EmpFileName = CDL2.FileName '** Remember employee file name
    Open EmpFileName For Input As #2 '** Open the employee file
    J = 1 '** Start with first employee
    Do Until EOF(2) '** Until the end of the file
    Input #2, EmployeeNum(J), EmployeeName(J), EmployeePass(J), ManagerStatus(J) '** Read J-th product
    J = J + 1 '** Anticipate next employee
    Loop


    NumEmployee = J - 1 '** Remember number of employees read
    Close #2


    TransFileName = CDL3.FileName '** Remember Transactions file name
    Open TransFileName For Input As #3 '** Open the Transactions file
    K = 1 '** Start with first Transaction
    Do Until EOF(3) '** Until the end of the file
    Input #3, TransDate(K), TransEmpNum(K), TransUPC(K), TransPrice(K) '** Read K-th product
    K = K + 1 '** Anticipate next Transaction
    Loop

    NumTrans = K - 1 '** Remember number of Transactions read
    Close #3


    Me.Show '** Show the cash register form
    cmdSignOn.SetFocus '** Set Focus to sign on button

    End Sub


    I couldn't paste the whole project. but that is where i get errors
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  15. #15
    Frenzied Member
    Join Date
    Dec 2000
    Posts
    1,195
    thats very very hard to read..

    post your code using
    ['vbcode]CODE IN HERE[/'vbcode]
    without the '

  16. #16

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    here is my module

    ['vbcode]
    Option Explicit

    '** The following constant is the width of the receipt in characters.
    '** Note, the receipt Text Box was widened and its point size decreased
    '** to 10. If you change the width or point size of the receipt Text Box,
    '** this constant must also be changed.
    Const ReceiptWidth As Integer = 29

    Const MaxProducts As Integer = 200 ' Maximum number of Products store can carry

    Const MaxEmployee As Integer = 12 ' Maximum number of Employees in the employee file

    Const MaxTrans As Double = 100000 ' Maximum number of Transactions that the store can do

    Const TaxRate As Currency = 0.065 ' Sales tax rate

    Global AmountTendered As Currency ' Amount of money offered by custmer to pay bill
    Global SubTotal As Double ' Running subtotal of items purchased
    Global SalesTax As Double ' Sales tax due on the order
    Global AmountDue As Single ' Total amount to charge customer = SubTotal + SalesTax

    '------Variables for the Products File---------

    Global UPC(MaxProducts) As String '** Array for UPC codes
    Global ProdDesc(MaxProducts) As String '** Product descriptions
    Global ProdPrice(MaxProducts) As Currency '** Product prices

    Global I As Integer '** Variable used to index these arrays
    Global NumProducts As Integer '** Variable providing number of products read.

    '------Variables for the Employee File---------

    Global EmployeeNum(MaxEmployee) As String '** This is the Employee's Number
    Global EmployeeName(MaxEmployee) As String '** This is the Employee's Name
    Global EmployeePass(MaxEmployee) As String '** This is the Employee's Password
    Global ManagerStatus(MaxEmployee) As Boolean '** This is to determine if the Employee has Manager Status/Rights

    Global J As Integer '** Variable used to index these arrays
    Global NumEmployee As Integer '** Variable providing number of Employees Logged in.

    '------Varibles for the Transactions File-------

    Global TransDate(MaxTrans) As Date '** This is the Date when the sale was made
    Global TransEmpNum(MaxTrans) As String '** This is the Employee Number of the individual who was logged in
    Global TransUPC(MaxTrans) As String '** This is the UPC of the product sold
    Global TransPrice(MaxTrans) As Currency '** This is the price of the item sold

    Global K As Integer '** Variable used to index these arrays
    Global NumTrans As Integer '** Variable providing number of Transactions preformed.

    '------Varibles to define the arrays------------

    Global ProdFileName As String '** Variable providing name of product file.
    Global EmpFileName As String '** Variable providing name of employee file.
    Global TransFileName As String '** Variable providing name of trans file.

    Function PL(A As String, I As Integer)
    Dim Result As String

    Result = A
    Do Until Len(Result) >= I
    Result = " " & Result




    Loop
    PL = Left(Result, I)

    End Function
    ['\vbcode]
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  17. #17

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    Sorry. Here is my module

    ['vbcode]
    Option Explicit

    '** The following constant is the width of the receipt in characters.
    '** Note, the receipt Text Box was widened and its point size decreased
    '** to 10. If you change the width or point size of the receipt Text Box,
    '** this constant must also be changed.
    Const ReceiptWidth As Integer = 29

    Const MaxProducts As Integer = 200 ' Maximum number of Products store can carry

    Const MaxEmployee As Integer = 12 ' Maximum number of Employees in the employee file

    Const MaxTrans As Double = 100000 ' Maximum number of Transactions that the store can do

    Const TaxRate As Currency = 0.065 ' Sales tax rate

    Global AmountTendered As Currency ' Amount of money offered by custmer to pay bill
    Global SubTotal As Double ' Running subtotal of items purchased
    Global SalesTax As Double ' Sales tax due on the order
    Global AmountDue As Single ' Total amount to charge customer = SubTotal + SalesTax

    '------Variables for the Products File---------

    Global UPC(MaxProducts) As String '** Array for UPC codes
    Global ProdDesc(MaxProducts) As String '** Product descriptions
    Global ProdPrice(MaxProducts) As Currency '** Product prices

    Global I As Integer '** Variable used to index these arrays
    Global NumProducts As Integer '** Variable providing number of products read.

    '------Variables for the Employee File---------

    Global EmployeeNum(MaxEmployee) As String '** This is the Employee's Number
    Global EmployeeName(MaxEmployee) As String '** This is the Employee's Name
    Global EmployeePass(MaxEmployee) As String '** This is the Employee's Password
    Global ManagerStatus(MaxEmployee) As Boolean '** This is to determine if the Employee has Manager Status/Rights

    Global J As Integer '** Variable used to index these arrays
    Global NumEmployee As Integer '** Variable providing number of Employees Logged in.

    '------Varibles for the Transactions File-------

    Global TransDate(MaxTrans) As Date '** This is the Date when the sale was made
    Global TransEmpNum(MaxTrans) As String '** This is the Employee Number of the individual who was logged in
    Global TransUPC(MaxTrans) As String '** This is the UPC of the product sold
    Global TransPrice(MaxTrans) As Currency '** This is the price of the item sold

    Global K As Integer '** Variable used to index these arrays
    Global NumTrans As Integer '** Variable providing number of Transactions preformed.

    '------Varibles to define the arrays------------

    Global ProdFileName As String '** Variable providing name of product file.
    Global EmpFileName As String '** Variable providing name of employee file.
    Global TransFileName As String '** Variable providing name of trans file.

    Function PL(A As String, I As Integer)
    Dim Result As String

    Result = A
    Do Until Len(Result) >= I
    Result = " " & Result




    Loop
    PL = Left(Result, I)

    End Function
    [/'vbcode]
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  18. #18

    Thread Starter
    Lively Member fujiyama17's Avatar
    Join Date
    Aug 2000
    Location
    Columbus, OH
    Posts
    91
    what???

    arrg. sorry
    fujiyama17 (representing KSU)
    -=-=-==-=-=
    "There is something funny about life. If you never settle for anything but the very best, you will very often recieve it."
    -Margaret M. Somet

  19. #19
    Hyperactive Member MikeBAM's Avatar
    Join Date
    Sep 2000
    Location
    Metro Detroit
    Posts
    284
    Try:

    Do While Not EOF(6)
    ~* )v( ! /< E *~

  20. #20
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Originally posted by ZeroCool
    This:
    VB Code:
    1. Do Until EOF(6) '** Until the end of the file
    Should Be this:
    VB Code:
    1. Do Until EOF(6)  = True

    They ARE the same

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