Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Number Generator? 1

Status
Not open for further replies.
Apr 3, 2003
180
0
0
US
Can someone please help with what I belive should be a simple problem? I need to add a number generator to an existing application. What I need is a button that once pressed it isues a 4 digit number that increments by one every time it is pressed. This number should come up in a text box that I will bind to a database. I need it to issue purchase order numbers starting with 1000 and increment by one. I have not written in .NET in two years and am a bit rusty, but I do not have much time to come up with this sollution. Any help would be greatly appreciated.

"I hear and I forget. I see and I remember. I do and I understand."
- Confucius (551 BC - 479)
 
Imports System.IO

Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim file_path As String = App_Path() & "\Counter.txt" ' Counter File
Dim objFileInfo As New FileInfo(file_path), num As Integer = 0

If objFileInfo.Exists Then
num = Get_File_Count(file_path)
TextBox1.Text = CStr(num)
num += 1
Put_File_Count(file_path, num)
Else
num = 1000
TextBox1.Text = CStr(num)
num += 1
Put_File_Count(file_path, num)
End If
End Sub

Private Function Get_File_Count(ByVal file_path As String) As Integer
Dim num As Integer = 0

FileOpen(1, file_path, OpenMode.Input)
num = CInt(LineInput(1))
FileClose(1)

Return num
End Function

Private Sub Put_File_Count(ByVal file_path As String, ByVal num As Integer)
FileOpen(1, file_path, OpenMode.Output)
Print(1, CStr(num))
FileClose(1)
End Sub

Private Function App_Path() As String
Return System.AppDomain.CurrentDomain.BaseDirectory()
End Function
End Class
 
Thank you so much for the example, it works great and is exactly what I was looking for.

"I hear and I forget. I see and I remember. I do and I understand."
- Confucius (551 BC - 479)
 
Just checking that you've got the best approach to your problem here -

If your application is updating a database, you would probably use some sort of autonumber for the purchase order.

For example in MSSQL Server you make the column an 'identity'. You would then have a stored procedure that handles the insert and returns the ID using the scope_identity function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top