Results 1 to 2 of 2
  1. #1
    Oct 2001

    storing a temp variable

    This *should* be easy, but I can't figure it out: I need to store values from textbox(es) into a temporary space so that I can recall the number after adding a new record. For instance, I have Purchase Orders and a details form underneath it. Once I hit Add Record, the current PO number disappears. I also want to be able to do some basic math for the Item Number textbox. So, if the PO # is 20002 and the last item is 4, I want to be able to hit new record and recall the PO # (20002) as well as add 1 to the item box (to make it 5, in this case). Thoughts? For the counter, I've tried this:

    dim i as integer
    i = txtItemNo.text
    if i <> 0 then
    i = i + 1
    i = 1
    end if
    The only problem is that I don't know exactly where to stick this. If I put it in Add_Record.OnClick, then it will (obviously) pull a zero, since the new record was created. I just need to be able to house these numbers temporarily so that I can recall them.

  2. #2
    Oct 2001
    Southern Ontario

    Re: storing a temp variable

    What you need to do is declare the variables properly first and then you'd be able to store them for as long as you need (or until the application is shut down).

    Declaring a variable can be as easy as:

    Dim I as Integer

    This tells VB (or VBA) to set aside memory for a variable 'I'. And it also tells VB what type of data is going to be stored in the variable. In this case, we are telling it that there are only going to be whole numbers (no decimal numbers).

    The most common types of variables are:
    • String - any text
    • Integer - Any whole number (positive or negative)
    • Long - Any number (positive/negative, whole or decimal)
    • Boolean - Yes/No

    Now depending on how long you would like to store each variable will determine where you place them in your code. Here's a snip of my code from an application. I've added comments (and deleted irrelivent stuff) to explain how the variables will last.
    Option Explicit  'Tells VB to force the user to delare EVERY variable.  This helps with debugging and is good practice
                         'even though it is not needed or required.
    'Standard vars
    Dim IsDirty As Boolean      'These variables are placed at the vary top of forum's code (prior to any subroutiens)
    Dim SubIsDirty As Boolean 'These variables last only while the forum is still open.  Once it is closed, all
    Dim Mode As Long            'these variables will be reset.
    Dim TableName As String
    Dim PKName As String
    Private Sub CmdPrintCRC_Click()
        Dim oAccess As Access.Application  'These three variables, oAccess, oReport, sSql will last only while the subroutien CmdPrintCRC_Click() is running.
        Dim oReport As Report                  'Once the sub has finished, the variables are delete and the memory is reclamed
        Dim sSql As String                        'Thus anything in here lasts only for one exicution of CmdPrintCRC_Click()
        On Error GoTo ErrorTrap
        Set oAccess = New Access.Application
    End Sub
    Private Sub CmdPrintFolder_Click()
    End Sub
    Now, if you need to have a variable last for the entire time your application is running, you will need to declare it within a module. I usially like to create a modual just to hold all of my Global variables. Here's what one of mine looks like:
    Option Explicit
    'Mode Constants
    Public Const MODE_FIND As Long = 0
    Public Const MODE_ADD As Long = 1
    Public Const MODE_EDIT As Long = 2
    Public Const MODE_DELETE As Long = 3
    Public Const MODE_PRINT As Long = 4
    'Size Constants
    Public Const BORDER3D_WIDTH As Long = 30
    'Database variables
    Public gcn As ADODB.Connection
    Public gDataPath As String
    Public gMenuPermissions As String
    Public gNavPermissions As String
    Public gControlPermissions As String
    Public gFilterPermissions As String
    Public gInquiryPermissions As String
    Public gShowNavigator As Boolean
    Public gClosePrint As Boolean
    Public gAccounting As Boolean
    Public gUserID As Long
    'System settings
    Public gCompName As String
    Public CustomerExpiryDate As Integer
    Public VenderExpiryDate As Integer
    Public MarkupRate As Long
    Public HourlyRate As Currency
    Public OldestTimeSheet As Integer
    Public DownTimeStart As Long
    Public DownTimeEnd As Long
    Public DownTimeSkipCount As Long
    'User setting variables
    Public LoginSucceeded As Boolean
    Public LoginID As String
    Public UrHistoryVisible As Boolean
    Public UrOldestJobToView As Long
    Public UrLockedColour As Long
    Public UrUnlockedColour As Long
    Public UrDeleteColour As Long
    Public UrDateFormat As String
    Public UrTimeFormat As String
    'Calender variables
    Public RequestDate As Date
    'History variables
    Public pbDetails As String
    'Accounting Variables
    Public gFreightIn As Long
    Public gAPTax As Long
    Public gARTax As Long
    Public gAPDisc As Long
    Public gARDisc As Long
    Public gSales As Long
    Public gAPMain As Long
    Public gARMain As Long
    Public gBank As Long
    Public gARTax2 As Long
    Public gJobPrefix As String
    Now you will notice that it's almost the same as before except that I am using the command Public instead of Dim. This tells VB to make these variables avaliable for the entire application to use.

    You will also notice that the first two groups of variables that I've got have the word Const in them. That changes those variables into Constants and then I assign a value to them. I can then recall that value anywhere in the program by simply calling that constant's name.

    There are a few other ways you can deal with variables, but this should be enough to get you by. Good luck!
    Last edited by Spankin Partier; 08-29-2005 at 06:55 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts