Solved

VB code logic

Posted on 2014-04-29
9
270 Views
Last Modified: 2014-05-03
Hi,

I'm creating a simple VB project which will run in the System Tray and will allow the user to generate an Account Number for new accounts.

The format of the account number will be TR000000

The first two letters will always be TR
The first two numbers will be the last two digits of the current year
The next two digits will be the current Month
The final two digits will be a consecutive number from 01-99

So if a user generates an account number on May 01 2014 the number will be:

TR140501

If another account number is generated on the following day it will be:

TR140502 & TR140503 & TR140504 etc, etc

On June 01 2014 the number would be:

TR140601 and so on.

I'm trying to get my head around how to generate the number accordingly.  I can sort of work out the Year and the Date section of it, but I'm stuck on the consecutive number.  How would I ensure that at the start of a new month the last two number of the account number would reset back to 01 and start all over again?

I'm planning on using an .ini file to store the currently generated number, but that's all I've got.

Hope someone can help me out with this.

Thanks
0
Comment
Question by:anthonytr
  • 5
  • 4
9 Comments
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40031082
Here's a function that can generate the next file name, including logic to look at the INI file.  See if this helps.

'EE28422600
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Define location of INI file
Const strIniFile = "B:\EE\EE28422600\EE28422600.ini"

' Testing calls to function
Wscript.Echo GetNextSeq()
Wscript.Echo GetNextSeq()
Wscript.Echo GetNextSeq()

' Generate the next file name to create
Function GetNextSeq()

   ' Build todays base for file name ("TRYYMM"), and default to seq number 1
   strBase = "TR" & Right(Year(Date), 2) & Right("0" & Month(Date), 2)
   intSeq = 1

   ' Create filesystem object
   Set objFSO = CreateObject("Scripting.FileSystemObject")

   ' If the INI exists, load it and determine next seq number to use
   If objFSO.FileExists(strIniFile) Then
      ' Read the last file name written
      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objFile = objFSO.OpenTextFile(strIniFile, ForReading, False, TriStateUseDefault)
      strText = objFile.ReadAll
      objFile.Close

      ' If the year and month match today then increment sequence number
      If strBase = Left(strText, 6) Then
         intSeq = CInt(Mid(strText, 7, 2)) + 1
      End If
   End If

   ' Add 2 digit sequence number to base of file name
   strBase = strBase & Right("0" & intSeq, 2)

   ' Rewrite INI file with this new file name
   Set objFile = objFSO.OpenTextFile(strIniFile, ForWriting, True)
   objFile.WriteLine strBase
   objFile.Close

   ' Return the new file name
   GetNextSeq = strBase

   ' Release objects
   Set objFile = Nothing
   Set objFSO = Nothing
End Function

Open in new window

~bp
0
 

Author Comment

by:anthonytr
ID: 40031379
Hi,

Thanks for this.  Would this be included on the OnClick event of my form/system tray?  After reading your code I'm just wondering where to put it. :-)
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 40031785
Yes, where ever you want to generate the file name.

Keep in mind this is VBScript code fore the example, you may have to adjust slightly for a VB project.

~bp
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:anthonytr
ID: 40033444
Hi,

Thanks for your replies and the code example. I have to say, I got lost at some points of your code.  I'll show you my current code which I put together.  Currently I can generate the TRYYMM bit of the code and could probably get the sequence number at the end, however, my problem/issue is making sure the sequence 'resets' to 01 at the beginning of each month.

My current code on the OnClick event is:

Private Sub GenerateTRNumberToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GenerateTRNumberToolStripMenuItem.Click
        Dim TRbase As String
        Dim TRmonth As String
        Dim TRYear As String
        Dim TRSeq As String
        Dim intSeq As String
        Dim Value As String


        'Generate the next TR number in the Sequence
        'Build base for the file name

        TRmonth = DateTime.Now.ToString("MM")
        TRYear = DateTime.Now.ToString("yy")

        TRbase = "TR" & TRmonth & TRYear

        'Check If the INI file exists, load it and determine next sequence number to use

        Dim objIniFile As New IniFile("C:\TRdata.ini")

        'Read the value from the INI file
        Value = objIniFile.GetString("TR Account Number", "Latest", "None")

        'If the year and month match today then increment sequence number by + 1
        'If the year and month do not match todays date then start the sequence back at 01


        Me.Show()
        Me.WindowState = FormWindowState.Normal
        NotifyIcon1.Visible = False
        Me.txt_TRNumber.Text = "TR" + TRYear + TRmonth
    End Sub

Open in new window


I have tried to set out what I looking at achieving.  I have the following code which write the generated number to the ini file.

TRnumber = Me.txt_TRNumber.Text

        Clipboard.Clear()   ' Clear Clipboard.
        Clipboard.SetText(txt_TRNumber.Text)   ' Put text on Clipboard.
        Me.WindowState = FormWindowState.Minimized ' minimise back to system tray
        Dim objIniFile As New IniFile("C:\TRdata.ini")
        objIniFile.WriteString("TR Account Number", "Latest", TRnumber)

Open in new window


If you could help with the sequence bit I would appreciate it.

Thanks
0
 

Author Comment

by:anthonytr
ID: 40033535
As an update, I now have the following code.

It appears to work, except there is one thing I can't get to work correctly.  The sequence part needs to be 01, 02, 03, 04 but currently it's just being generated 1, 2, 3, 4

How would I format the sequence so that it has the leading zero?

Private Sub GenerateTRNumberToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GenerateTRNumberToolStripMenuItem.Click
        Dim TRbase As String
        Dim TRmonth As String
        Dim TRYear As String
        Dim TRSeq As String
        Dim intSeq As String
        Dim iniMonth As String
        Dim iniYear As String
        Dim iniSeq As String


        'Generate the next TR number in the Sequence
        'Build base for the file name

        TRmonth = DateTime.Now.ToString("MM")
        TRYear = DateTime.Now.ToString("yy")
        TRbase = "TR" & TRmonth & TRYear

        'Check If the INI file exists, load it and determine next sequence number to use

        Dim objIniFile As New IniFile("C:\TRdata.ini")

        'Read the value from the INI file
        iniMonth = objIniFile.GetString("Month", "Latest", "None")
        iniYear = objIniFile.GetString("Year", "Latest", "None")
        iniSeq = objIniFile.GetString("Sequence", "Latest", "None")

        If iniMonth = DateTime.Now.ToString("MM") Then
            TRSeq = iniSeq + 1
        Else
            TRSeq = "01"
        End If

        Me.Show()
        Me.WindowState = FormWindowState.Normal
        NotifyIcon1.Visible = False
        Me.txt_TRNumber.Text = "TR" + TRYear + TRmonth + TRSeq

    End Sub

Open in new window

0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 40033546
This should pad left with 0 to get 2 digits.

TRSeq.ToString("D2")

~bp
0
 

Author Comment

by:anthonytr
ID: 40033559
That's what i have been playing with but i keep getting runtime errors.  Converting String to IFormatProvider.

Don't know what i'm doing here wrong.  Could you show where and how in my code i would use it?
0
 

Author Comment

by:anthonytr
ID: 40033589
I have it working with the below code:

    Private Sub GenerateTRNumberToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles GenerateTRNumberToolStripMenuItem.Click
        Dim TRbase As String
        Dim TRmonth As String
        Dim TRYear As String
        Dim TRSeq As String
        Dim intSeq As Integer
        Dim iniMonth As String
        Dim iniYear As String
        Dim iniSeq As Integer


        'Generate the next TR number in the Sequence
        'Build base for the file name

        TRmonth = DateTime.Now.ToString("MM")
        TRYear = DateTime.Now.ToString("yy")
        TRbase = "TR" & TRmonth & TRYear

        'Check If the INI file exists, load it and determine next sequence number to use

        Dim objIniFile As New IniFile("C:\TRdata.ini")

        'Read the value from the INI file
        iniMonth = objIniFile.GetString("Month", "Latest", "None")
        iniYear = objIniFile.GetString("Year", "Latest", "None")
        iniSeq = objIniFile.GetString("Sequence", "Latest", "None")

        If iniMonth = DateTime.Now.ToString("MM") Then
            intSeq = iniSeq + 1
            TRSeq = (intSeq.ToString("D2"))

        Else
            TRSeq = "01"
        End If

        Me.Show()
        Me.WindowState = FormWindowState.Normal
        NotifyIcon1.Visible = False
        Me.txt_TRNumber.Text = "TR" + TRYear + TRmonth + TRSeq

    End Sub

Open in new window


Does this code look ok?  I was't sure if it was a bit "Heath Robinson"  Is there a simpler way of achieving the same thing?
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 40033602
Doesn't look bad to me.

~bp
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question