Solved

VB code logic

Posted on 2014-04-29
9
266 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 52

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 52

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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 52

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 52

Expert Comment

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

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

914 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now