Solved

VB code logic

Posted on 2014-04-29
9
261 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 51

Accepted Solution

by:
Bill Prew earned 500 total points
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:anthonytr
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
This should pad left with 0 to get 2 digits.

TRSeq.ToString("D2")

~bp
0
 

Author Comment

by:anthonytr
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
Doesn't look bad to me.

~bp
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
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 …

762 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

10 Experts available now in Live!

Get 1:1 Help Now