Hey guys!
I've been trying to make an inventory management program for a visual basic class I'm taking. I've been having some difficulties creating a file location procedure which occurs during the application loading process. Its purpose is to check a registry key (if one exists) to see if it points to a valid file and if it doesn't provide a form which offers options to either create a new excel file or to select a file manually and update the registry key. Once the registry key is set the main program is supposed to load.
It appears all of my code is functional, yet when I attempt to Form1.Show and Me.Close the filelocation form (Dialog1) I receive the following error:
Cannot access a disposed object.
Object name: 'Dialog1'.
Previously I attempted to use Dialog1.Hide but to no avail, that form will not disappear from my screen.
I have sought out anything that might relate back to Dialog1 from within Form1, but I couldn't locate anything. If you can help I would greatly appreciate it! I'd really like to understand what's going on here, not just for my class, but for my own knowledge.
Thank you very much!
Dialog1.vb is set to load as the "startup form"
"Shutdown mode" has been set to "When last form closes"
Download Full Solution (.rar)
Upload Files, MP3, Music. Free File Hosting To Share Files
Code:
Dialog1.vb
Imports Microsoft.Office.Interop.Excel ' excel extension
Imports Microsoft.Win32 ' windows registry key access (Microsoft Windows Operating System "built in database"
' - explore using
' - used in this instance for storing the location (as a string) of the data.xlsx file
Imports System
Imports System.IO ' Windows file system
Public Class Dialog1
Public filelocation As String
Dim oExcel As Object = CreateObject("Excel.Application") 'application
Dim FileConfig As Object 'file .xlsx
Dim oBookConfig As Object 'worksheet 1
Dim oBookInventory As Object 'worksheet 2
Dim oBookComplaints As Object 'worksheet 3
Dim registrykey As RegistryKey = Registry.CurrentUser.CreateSubKey("InventoryManager")
Dim filelocationstatus As String
Private Sub Dialog1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If registrykey.GetValue("filelocation") Is Nothing Then
MsgBox("No Key")
filelocationstatus = "nokey"
Else
If File.Exists(registrykey.GetValue("filelocation")) = True Then
filelocationstatus = "goodkey"
MsgBox("Good Key")
Else
filelocationstatus = "badkey"
MsgBox("Bad Key")
End If
End If
'key not present (New User) (Create Key) - NOKEY
If filelocationstatus = "nokey" Then
MsgBox("Activated - No Key")
Me.Text = "Welcome!"
RichTextBox1.Text = "This appears to be your first time running Inventory Manager. If you don't already have a data storage file click Create New. If you already have a data file from previous use click Enter Location."
GoTo jump
End If
'key present - no file (Reset Key) - BADKEY
If filelocationstatus = "badkey" Then
MsgBox("Activated - Bad Key")
Me.Text = "Error"
RichTextBox1.Text = "Could not locate your data file. You can either click Create New to create a new data file or you can click Select Location to manually enter your file's location."
GoTo jump
End If
'key good
MsgBox("Activated - goodkey")
If filelocationstatus = "goodkey" Then
Form1.Show()
Me.Close()
End If
jump:
End Sub
' button: "Enter Location"
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
'Enter Location Button
'Select file and set as filelocation
Dim openFileDailog1 As New OpenFileDialog()
openFileDailog1.Filter = "Excel Spreadsheet|*.xlsx"
openFileDailog1.Title = "Open Data File..."
openFileDailog1.ShowDialog()
filelocation = openFileDailog1.FileName
'create registry key if necesary, set registry key value
If registrykey.GetValue("filelocation") Is Nothing Then
registrykey.SetValue("filelocation", filelocation)
End If
registrykey.SetValue("filelocation", filelocation)
Form1.Show()
Me.Hide()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
'Create New button
'create new data.xlsx in user's documents folder
'works like a temporary cache
Dim filelocation As String = Environment.SpecialFolder.MyDocuments & "/data.xlsx"
Dim misValue As Object = System.Reflection.Missing.Value
Dim oExcel As Object = CreateObject("Excel.Application")
Dim FileConfig As Object = oExcel.Workbooks.Add(misValue)
Dim oBookConfig As Object = FileConfig.Worksheets(1) 'worksheet 1
Dim oBookInventory As Object = FileConfig.Worksheets(2) 'worksheet 2
Dim oBookComplaints As Object = FileConfig.Worksheets(3) 'worksheet 3
oBookConfig.Cells(3, 2).Value = 0
oBookConfig.Cells(4, 2).Value = 0
registrykey.SetValue("filelocation", filelocation)
'create data.xlsx
Try
MsgBox(filelocation)
oBookInventory.Saveas(filelocation)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'close form
Form1.Show()
Me.Visible = False
jump:
End Sub
End Class
Form1.vb
Imports Microsoft.Office.Interop.Excel ' Microsoft Excel extension
Imports Microsoft.Win32 ' Windows registry key access (Microsoft Windows Operating System's "built in local database")
' - explore using START MENU>>Run>>regedit.exe
' - used in this instance for storing the location (as a string) of the data.xlsx file
Imports System
Imports System.IO ' Windows file system
Public Class Form1
Dim registrykey As RegistryKey = Registry.CurrentUser.CreateSubKey("InventoryManager")
Dim filelocation As String
Dim oExcel As Object = CreateObject("Excel.Application") 'application
Dim FileConfig As Object 'file .xlsx
Dim oBookConfig As Object 'worksheet 1
Dim oBookInventory As Object 'worksheet 2
Dim oBookComplaints As Object 'worksheet 3
Dim itemcount As Integer 'quantity of items in list
Dim complaintcount As Integer 'quantity of complaint items listed
Dim SIERow As Integer = 0 'Selected Item's Excel Row; call update_SIERow prior to use
Dim IAMCount As Integer = 0 'Item Array Member Count; contains the second field of the arrays count
Dim itemarray(4, 0) As String
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
filelocation = registrykey.GetValue("filelocation")
'Define Excel variables
FileConfig = oExcel.Workbooks.Add(filelocation)
oBookConfig = FileConfig.Worksheets(1)
oBookInventory = FileConfig.Worksheets(2)
oBookComplaints = FileConfig.Worksheets(3)
'Define globally used variables
itemcount = oBookConfig.Cells(3, 2).Value()
complaintcount = oBookConfig.Cells(4, 2).Value()
Call reload_List()
oExcel.DisplayAlerts = False
'set up listveiw
ListView1.View = View.Details
ListView1.GridLines = False
ListView1.MultiSelect = False
ListView1.Sorting = SortOrder.Ascending
ListView1.Columns.Add("Items", 140, HorizontalAlignment.Left)
ListView1.Columns.Add("Quantity", 70, HorizontalAlignment.Center)
ListView1.Columns.Add("In", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("Out", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("Price", 70, HorizontalAlignment.Center)
jump:
End Sub 'array containing # from data.xlsx list order
' - item @ itemarray(0,x)
' - whole quantity @ itemarray(1,x)
' - quantity in stock @ itemarray(2,x)
' - quantity out of stock @ itemarray(3,x)
' - price per unit @ itemarray(4,x)
'*************ROUTINES
' routine: Form1 Showing (Form1_Load)
' routine: ListVeiw1 - Selected Index Changed
Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
update_SIERow()
'fill update item panel
Dim item As String
Dim itemquantity As Integer
Dim itemin As Integer
Dim itemout As Integer
Dim itemprice As Double
If Not SIERow = 0 Then
item = oBookInventory.Cells(SIERow, 1).Value
itemquantity = oBookInventory.Cells(SIERow, 2).Value
itemin = oBookInventory.Cells(SIERow, 3).Value
itemout = oBookInventory.Cells(SIERow, 4).Value
itemprice = oBookInventory.Cells(SIERow, 5).Value
TextBox2.Text = item
NumericUpDown5.Value = itemquantity
NumericUpDown6.Value = itemin
NumericUpDown7.Value = itemout
NumericUpDown8.Value = itemprice
End If
End Sub
'*************END ROUTINES
'*************PROCEDURES
Sub reload_List()
'Refresh ListVeiw1 from data.xlsx
' 1) clear listveiw1 contents
' 2) clear itemarray() contents
' 3) (re)load listveiw1.items with contents from data.xlsx list
' 4) (re)load itemarray() with contents from data.xlsx
' 5) update StripStatus Label w/ ItemCount & ItemArray()
'1) clear listveiw1 contents
ListView1.Items.Clear()
'2) clear itemarray() contents
Array.Clear(itemarray, itemarray.GetLowerBound(0), itemarray.Length)
Dim ListItemData(5) As String ' <====== Quick Private Array as Short Term Cache
Dim ListItem As ListViewItem
Dim item As String = ""
Dim itemquantity As Integer
Dim itemin As Integer
Dim itemout As Integer
Dim itemprice As Double
If Not itemcount = 0 Then
For x = 0 To itemcount - 1
Try
' 3) (re)load listveiw1.items with contents from data.xlsx list
item = oBookInventory.Cells(x + 3, 1).Value
itemquantity = oBookInventory.Cells(x + 3, 2).Value
itemin = oBookInventory.Cells(x + 3, 3).Value
itemout = oBookInventory.Cells(x + 3, 4).Value
itemprice = oBookInventory.Cells(x + 3, 5).Value
Catch ex As Exception
MsgBox("reload_list Procedure" & vbNewLine & "Failed to access data.xlsx while attempting to reload lists", , "Error 3")
GoTo jump
End Try
Try
ListItemData(0) = item
ListItemData(1) = itemquantity
ListItemData(2) = itemin
ListItemData(3) = itemout
ListItemData(4) = itemprice
ListItem = New ListViewItem(ListItemData)
ListView1.Items.Add(ListItem)
Catch ex As Exception
MsgBox("reload_list Procedure" & vbNewLine & "Failed to load listveiw1 with items from data.xlsx", , "Error")
GoTo jump
End Try
Try
'4) (re)load itemarray() with contents from data.xlsx
' item
ReDim Preserve itemarray(4, x + 1)
itemarray(0, x) = item
' whole quantity
itemarray(1, x) = itemquantity
' quantity in stock
itemarray(2, x) = itemin
' quantity out of stock
itemarray(3, x) = itemout
' price per unit
itemarray(4, x) = itemprice
IAMCount = x
ReDim Preserve itemarray(4, x + 1)
Catch ex As Exception
MsgBox("reload_list Procedure" & vbNewLine & "Failed to load itemarray() with items from data.xlsx", , "Error")
GoTo jump
End Try
'5) update StripStatus Label w/ ItemCount & ItemArray
update_ToolStripStatus()
Next
End If
jump:
End Sub ' procedure: reload_List
' procedure: update_ComplaintCountAndSave(+/- # integer)
Sub update_ComplaintCountANDSave(ByVal difference)
If IsNumeric(difference) Then
Try
oBookConfig.Cells(4, 2).Value = complaintcount + difference
complaintcount = complaintcount + difference
oBookComplaints.Saveas(filelocation)
Catch ex As Exception
MsgBox("update_ComplaintCountANDSave Function", , "Error: update_ComplaintCountANDSave Function")
End Try
End If
End Sub
' procedure: update_ItemCountAndSave(+/- # integer)
Sub update_ItemCountANDSave(ByVal difference)
If IsNumeric(difference) Then
Try
oBookConfig.Cells(3, 2).Value = itemcount + difference
itemcount = itemcount + difference
oBookInventory.Saveas(filelocation)
Catch ex As Exception
MsgBox("Unable to save - Is the file already open?", , "Error: update_ItemCountANDSave Function")
End Try
End If
End Sub
Sub update_SIERow()
'return row # of selected list item
Dim item As String
Dim itemnumber As Integer
Try
For itemnum = 3 To itemcount + 3
item = oBookInventory.Cells(itemnum, 1).Value
itemnumber = itemnum
If ListView1.SelectedItems.Item(0).Text = item Then
SIERow = itemnum
update_ToolStripStatus()
GoTo jump
End If
Next
MsgBox(" update_SIERow() did not find your list item.", , "Error")
Catch ex As Exception
update_ToolStripStatus()
'MsgBox("itemnumber: " & itemnumber & vbNewLine & ex.ToString, , "Error: update_SelectedItemExcelRow Function")
'THERES AN ERROR IN HERE I CANT FIND BUT IT ALL WORKS ANYWAY
End Try
jump:
End Sub ' procedure: update_SIERow(Selected Item's Excel Row)
Sub update_ToolStripStatus()
ToolStripStatusLabel1.Text = "ItemCount: " & itemcount & " " & "ItemArray: (4," & IAMCount & ") SIERow: " & SIERow
End Sub ' procedure: update_ToolStripStatus
'*************END PROCEDURES
'*************FUNCTIONS
' function: check_IfItemExists
Private Function check_IfItemExists(ByVal item) ' compares string with item names in data.xlsx; if exists returns true
Return False
Try
For Each Str As String In itemarray
If Str.Contains(item) Then
MsgBox("Found " & item & " at index " &
Str.IndexOf(item), , "Duplicate Found")
Return True
End If
Next
Catch ex As Exception
MsgBox("Error with function check_IfItemExists(ByVal item)", , "Error")
End Try
End Function
'*************END FUNCTIONS
'*************BUTTONS
' button: "Add Item"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'add item to data.xlsx, then reload_list from data.xlsx
Dim item As String
Dim itemquantity As Integer
Dim itemin As Integer
Dim itemout As Integer
Dim itemprice As Double
item = TextBox1.Text
itemquantity = NumericUpDown1.Value
itemin = NumericUpDown2.Value
itemout = NumericUpDown3.Value
itemprice = NumericUpDown4.Value
If check_IfItemExists(item) = True Then
MsgBox("This item already exists.", , "Error")
GoTo jump
End If
Try
If Not itemin + itemout = itemquantity Then
MsgBox("The amount of items in & out of stock do not total the whole quantity of items")
GoTo jump
End If
Catch ex As Exception
MsgBox("Could not add integers.", , "Error")
GoTo jump
End Try
Try
oBookInventory.Cells(itemcount + 3, 1).Value = item
oBookInventory.Cells(itemcount + 3, 2).Value = itemquantity
oBookInventory.Cells(itemcount + 3, 3).Value = itemin
oBookInventory.Cells(itemcount + 3, 4).Value = itemout
oBookInventory.Cells(itemcount + 3, 5).Value = itemprice
Catch ex As Exception
MsgBox("Could not write to data.xlsx", , "Error")
GoTo jump
End Try
update_ItemCountANDSave(+1)
reload_List()
TextBox1.Clear()
NumericUpDown1.Value = 0
NumericUpDown2.Value = 0
NumericUpDown3.Value = 0
jump:
End Sub
' button: "Remove Item"
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'remove item
If MsgBox("Are you sure you would like to delete this item?", MsgBoxStyle.YesNo, "Delete") = MsgBoxResult.Yes Then
Try
oBookInventory.rows(SIERow).delete()
Catch ex As Exception
MsgBox("Did not remove row from data.xls", , "Error: Button Remove Item")
End Try
update_ItemCountANDSave(-1)
reload_List()
End If
End Sub
' button: "Update Item" (Edit)
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
update_SIERow()
Dim item As String
Dim itemquantity As Integer
Dim itemin As Integer
Dim itemout As Integer
Dim itemprice As Double
item = TextBox2.Text
itemquantity = NumericUpDown5.Value
itemin = NumericUpDown6.Value
itemout = NumericUpDown7.Value
itemprice = NumericUpDown8.Value
Try
If Not itemin + itemout = itemquantity Then
MsgBox("To save the amount of items in & out of stock do not total the whole quantity of items", , "Error")
GoTo jump
End If
Catch ex As Exception
MsgBox("Could not add integers.", , "Error")
End Try
If MsgBox("Are you sure you would like to apply this update?", MsgBoxStyle.YesNo, "Check") = MsgBoxResult.Yes Then
Try
oBookInventory.Cells(SIERow, 1).Value = item
oBookInventory.Cells(SIERow, 2).Value = itemquantity
oBookInventory.Cells(SIERow, 3).Value = itemin
oBookInventory.Cells(SIERow, 4).Value = itemout
oBookInventory.Cells(SIERow, 5).Value = itemprice
update_ItemCountANDSave(0)
reload_List()
Catch ex As Exception
MsgBox("Could not write to data.xlsx", , "Error")
End Try
End If
jump:
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Define Excel variables
FileConfig = oExcel.Workbooks.Add(filelocation)
oBookConfig = FileConfig.Worksheets(1)
oBookInventory = FileConfig.Worksheets(2)
oBookComplaints = FileConfig.Worksheets(3)
'Define globally used variables
itemcount = oBookConfig.Cells(3, 2).Value()
complaintcount = oBookConfig.Cells(4, 2).Value()
Call reload_List()
oExcel.DisplayAlerts = False
'set up listveiw
ListView1.View = View.Details
ListView1.GridLines = False
ListView1.MultiSelect = False
ListView1.Sorting = SortOrder.Ascending
ListView1.Columns.Add("Items", 140, HorizontalAlignment.Left)
ListView1.Columns.Add("Quantity", 70, HorizontalAlignment.Center)
ListView1.Columns.Add("In", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("Out", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("Price", 70, HorizontalAlignment.Center)
jump:
End Sub
'*************END BUTTONS
' *** *** *** Under Construction *** *** ***
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'Define Excel variables
FileConfig = oExcel.Workbooks.Add(filelocation)
oBookConfig = FileConfig.Worksheets(1)
oBookInventory = FileConfig.Worksheets(2)
oBookComplaints = FileConfig.Worksheets(3)
'Define globally used variables
itemcount = oBookConfig.Cells(3, 2).Value()
complaintcount = oBookConfig.Cells(4, 2).Value()
Call reload_List()
oExcel.DisplayAlerts = False
'set up listveiw
ListView1.View = View.Details
ListView1.GridLines = False
ListView1.MultiSelect = False
ListView1.Sorting = SortOrder.Ascending
ListView1.Columns.Add("Items", 140, HorizontalAlignment.Left)
ListView1.Columns.Add("Quantity", 70, HorizontalAlignment.Center)
ListView1.Columns.Add("In", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("Out", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("Price", 70, HorizontalAlignment.Center)
jump:
End Sub
End Class
I've been trying to make an inventory management program for a visual basic class I'm taking. I've been having some difficulties creating a file location procedure which occurs during the application loading process. Its purpose is to check a registry key (if one exists) to see if it points to a valid file and if it doesn't provide a form which offers options to either create a new excel file or to select a file manually and update the registry key. Once the registry key is set the main program is supposed to load.
It appears all of my code is functional, yet when I attempt to Form1.Show and Me.Close the filelocation form (Dialog1) I receive the following error:
Cannot access a disposed object.
Object name: 'Dialog1'.
Previously I attempted to use Dialog1.Hide but to no avail, that form will not disappear from my screen.
I have sought out anything that might relate back to Dialog1 from within Form1, but I couldn't locate anything. If you can help I would greatly appreciate it! I'd really like to understand what's going on here, not just for my class, but for my own knowledge.
Thank you very much!
Dialog1.vb is set to load as the "startup form"
"Shutdown mode" has been set to "When last form closes"
Download Full Solution (.rar)
Upload Files, MP3, Music. Free File Hosting To Share Files
Code:
Dialog1.vb
Imports Microsoft.Office.Interop.Excel ' excel extension
Imports Microsoft.Win32 ' windows registry key access (Microsoft Windows Operating System "built in database"
' - explore using
' - used in this instance for storing the location (as a string) of the data.xlsx file
Imports System
Imports System.IO ' Windows file system
Public Class Dialog1
Public filelocation As String
Dim oExcel As Object = CreateObject("Excel.Application") 'application
Dim FileConfig As Object 'file .xlsx
Dim oBookConfig As Object 'worksheet 1
Dim oBookInventory As Object 'worksheet 2
Dim oBookComplaints As Object 'worksheet 3
Dim registrykey As RegistryKey = Registry.CurrentUser.CreateSubKey("InventoryManager")
Dim filelocationstatus As String
Private Sub Dialog1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If registrykey.GetValue("filelocation") Is Nothing Then
MsgBox("No Key")
filelocationstatus = "nokey"
Else
If File.Exists(registrykey.GetValue("filelocation")) = True Then
filelocationstatus = "goodkey"
MsgBox("Good Key")
Else
filelocationstatus = "badkey"
MsgBox("Bad Key")
End If
End If
'key not present (New User) (Create Key) - NOKEY
If filelocationstatus = "nokey" Then
MsgBox("Activated - No Key")
Me.Text = "Welcome!"
RichTextBox1.Text = "This appears to be your first time running Inventory Manager. If you don't already have a data storage file click Create New. If you already have a data file from previous use click Enter Location."
GoTo jump
End If
'key present - no file (Reset Key) - BADKEY
If filelocationstatus = "badkey" Then
MsgBox("Activated - Bad Key")
Me.Text = "Error"
RichTextBox1.Text = "Could not locate your data file. You can either click Create New to create a new data file or you can click Select Location to manually enter your file's location."
GoTo jump
End If
'key good
MsgBox("Activated - goodkey")
If filelocationstatus = "goodkey" Then
Form1.Show()
Me.Close()
End If
jump:
End Sub
' button: "Enter Location"
Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
'Enter Location Button
'Select file and set as filelocation
Dim openFileDailog1 As New OpenFileDialog()
openFileDailog1.Filter = "Excel Spreadsheet|*.xlsx"
openFileDailog1.Title = "Open Data File..."
openFileDailog1.ShowDialog()
filelocation = openFileDailog1.FileName
'create registry key if necesary, set registry key value
If registrykey.GetValue("filelocation") Is Nothing Then
registrykey.SetValue("filelocation", filelocation)
End If
registrykey.SetValue("filelocation", filelocation)
Form1.Show()
Me.Hide()
End Sub
Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
'Create New button
'create new data.xlsx in user's documents folder
'works like a temporary cache
Dim filelocation As String = Environment.SpecialFolder.MyDocuments & "/data.xlsx"
Dim misValue As Object = System.Reflection.Missing.Value
Dim oExcel As Object = CreateObject("Excel.Application")
Dim FileConfig As Object = oExcel.Workbooks.Add(misValue)
Dim oBookConfig As Object = FileConfig.Worksheets(1) 'worksheet 1
Dim oBookInventory As Object = FileConfig.Worksheets(2) 'worksheet 2
Dim oBookComplaints As Object = FileConfig.Worksheets(3) 'worksheet 3
oBookConfig.Cells(3, 2).Value = 0
oBookConfig.Cells(4, 2).Value = 0
registrykey.SetValue("filelocation", filelocation)
'create data.xlsx
Try
MsgBox(filelocation)
oBookInventory.Saveas(filelocation)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'close form
Form1.Show()
Me.Visible = False
jump:
End Sub
End Class
Form1.vb
Imports Microsoft.Office.Interop.Excel ' Microsoft Excel extension
Imports Microsoft.Win32 ' Windows registry key access (Microsoft Windows Operating System's "built in local database")
' - explore using START MENU>>Run>>regedit.exe
' - used in this instance for storing the location (as a string) of the data.xlsx file
Imports System
Imports System.IO ' Windows file system
Public Class Form1
Dim registrykey As RegistryKey = Registry.CurrentUser.CreateSubKey("InventoryManager")
Dim filelocation As String
Dim oExcel As Object = CreateObject("Excel.Application") 'application
Dim FileConfig As Object 'file .xlsx
Dim oBookConfig As Object 'worksheet 1
Dim oBookInventory As Object 'worksheet 2
Dim oBookComplaints As Object 'worksheet 3
Dim itemcount As Integer 'quantity of items in list
Dim complaintcount As Integer 'quantity of complaint items listed
Dim SIERow As Integer = 0 'Selected Item's Excel Row; call update_SIERow prior to use
Dim IAMCount As Integer = 0 'Item Array Member Count; contains the second field of the arrays count
Dim itemarray(4, 0) As String
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
filelocation = registrykey.GetValue("filelocation")
'Define Excel variables
FileConfig = oExcel.Workbooks.Add(filelocation)
oBookConfig = FileConfig.Worksheets(1)
oBookInventory = FileConfig.Worksheets(2)
oBookComplaints = FileConfig.Worksheets(3)
'Define globally used variables
itemcount = oBookConfig.Cells(3, 2).Value()
complaintcount = oBookConfig.Cells(4, 2).Value()
Call reload_List()
oExcel.DisplayAlerts = False
'set up listveiw
ListView1.View = View.Details
ListView1.GridLines = False
ListView1.MultiSelect = False
ListView1.Sorting = SortOrder.Ascending
ListView1.Columns.Add("Items", 140, HorizontalAlignment.Left)
ListView1.Columns.Add("Quantity", 70, HorizontalAlignment.Center)
ListView1.Columns.Add("In", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("Out", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("Price", 70, HorizontalAlignment.Center)
jump:
End Sub 'array containing # from data.xlsx list order
' - item @ itemarray(0,x)
' - whole quantity @ itemarray(1,x)
' - quantity in stock @ itemarray(2,x)
' - quantity out of stock @ itemarray(3,x)
' - price per unit @ itemarray(4,x)
'*************ROUTINES
' routine: Form1 Showing (Form1_Load)
' routine: ListVeiw1 - Selected Index Changed
Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListView1.ItemSelectionChanged
update_SIERow()
'fill update item panel
Dim item As String
Dim itemquantity As Integer
Dim itemin As Integer
Dim itemout As Integer
Dim itemprice As Double
If Not SIERow = 0 Then
item = oBookInventory.Cells(SIERow, 1).Value
itemquantity = oBookInventory.Cells(SIERow, 2).Value
itemin = oBookInventory.Cells(SIERow, 3).Value
itemout = oBookInventory.Cells(SIERow, 4).Value
itemprice = oBookInventory.Cells(SIERow, 5).Value
TextBox2.Text = item
NumericUpDown5.Value = itemquantity
NumericUpDown6.Value = itemin
NumericUpDown7.Value = itemout
NumericUpDown8.Value = itemprice
End If
End Sub
'*************END ROUTINES
'*************PROCEDURES
Sub reload_List()
'Refresh ListVeiw1 from data.xlsx
' 1) clear listveiw1 contents
' 2) clear itemarray() contents
' 3) (re)load listveiw1.items with contents from data.xlsx list
' 4) (re)load itemarray() with contents from data.xlsx
' 5) update StripStatus Label w/ ItemCount & ItemArray()
'1) clear listveiw1 contents
ListView1.Items.Clear()
'2) clear itemarray() contents
Array.Clear(itemarray, itemarray.GetLowerBound(0), itemarray.Length)
Dim ListItemData(5) As String ' <====== Quick Private Array as Short Term Cache
Dim ListItem As ListViewItem
Dim item As String = ""
Dim itemquantity As Integer
Dim itemin As Integer
Dim itemout As Integer
Dim itemprice As Double
If Not itemcount = 0 Then
For x = 0 To itemcount - 1
Try
' 3) (re)load listveiw1.items with contents from data.xlsx list
item = oBookInventory.Cells(x + 3, 1).Value
itemquantity = oBookInventory.Cells(x + 3, 2).Value
itemin = oBookInventory.Cells(x + 3, 3).Value
itemout = oBookInventory.Cells(x + 3, 4).Value
itemprice = oBookInventory.Cells(x + 3, 5).Value
Catch ex As Exception
MsgBox("reload_list Procedure" & vbNewLine & "Failed to access data.xlsx while attempting to reload lists", , "Error 3")
GoTo jump
End Try
Try
ListItemData(0) = item
ListItemData(1) = itemquantity
ListItemData(2) = itemin
ListItemData(3) = itemout
ListItemData(4) = itemprice
ListItem = New ListViewItem(ListItemData)
ListView1.Items.Add(ListItem)
Catch ex As Exception
MsgBox("reload_list Procedure" & vbNewLine & "Failed to load listveiw1 with items from data.xlsx", , "Error")
GoTo jump
End Try
Try
'4) (re)load itemarray() with contents from data.xlsx
' item
ReDim Preserve itemarray(4, x + 1)
itemarray(0, x) = item
' whole quantity
itemarray(1, x) = itemquantity
' quantity in stock
itemarray(2, x) = itemin
' quantity out of stock
itemarray(3, x) = itemout
' price per unit
itemarray(4, x) = itemprice
IAMCount = x
ReDim Preserve itemarray(4, x + 1)
Catch ex As Exception
MsgBox("reload_list Procedure" & vbNewLine & "Failed to load itemarray() with items from data.xlsx", , "Error")
GoTo jump
End Try
'5) update StripStatus Label w/ ItemCount & ItemArray
update_ToolStripStatus()
Next
End If
jump:
End Sub ' procedure: reload_List
' procedure: update_ComplaintCountAndSave(+/- # integer)
Sub update_ComplaintCountANDSave(ByVal difference)
If IsNumeric(difference) Then
Try
oBookConfig.Cells(4, 2).Value = complaintcount + difference
complaintcount = complaintcount + difference
oBookComplaints.Saveas(filelocation)
Catch ex As Exception
MsgBox("update_ComplaintCountANDSave Function", , "Error: update_ComplaintCountANDSave Function")
End Try
End If
End Sub
' procedure: update_ItemCountAndSave(+/- # integer)
Sub update_ItemCountANDSave(ByVal difference)
If IsNumeric(difference) Then
Try
oBookConfig.Cells(3, 2).Value = itemcount + difference
itemcount = itemcount + difference
oBookInventory.Saveas(filelocation)
Catch ex As Exception
MsgBox("Unable to save - Is the file already open?", , "Error: update_ItemCountANDSave Function")
End Try
End If
End Sub
Sub update_SIERow()
'return row # of selected list item
Dim item As String
Dim itemnumber As Integer
Try
For itemnum = 3 To itemcount + 3
item = oBookInventory.Cells(itemnum, 1).Value
itemnumber = itemnum
If ListView1.SelectedItems.Item(0).Text = item Then
SIERow = itemnum
update_ToolStripStatus()
GoTo jump
End If
Next
MsgBox(" update_SIERow() did not find your list item.", , "Error")
Catch ex As Exception
update_ToolStripStatus()
'MsgBox("itemnumber: " & itemnumber & vbNewLine & ex.ToString, , "Error: update_SelectedItemExcelRow Function")
'THERES AN ERROR IN HERE I CANT FIND BUT IT ALL WORKS ANYWAY
End Try
jump:
End Sub ' procedure: update_SIERow(Selected Item's Excel Row)
Sub update_ToolStripStatus()
ToolStripStatusLabel1.Text = "ItemCount: " & itemcount & " " & "ItemArray: (4," & IAMCount & ") SIERow: " & SIERow
End Sub ' procedure: update_ToolStripStatus
'*************END PROCEDURES
'*************FUNCTIONS
' function: check_IfItemExists
Private Function check_IfItemExists(ByVal item) ' compares string with item names in data.xlsx; if exists returns true
Return False
Try
For Each Str As String In itemarray
If Str.Contains(item) Then
MsgBox("Found " & item & " at index " &
Str.IndexOf(item), , "Duplicate Found")
Return True
End If
Next
Catch ex As Exception
MsgBox("Error with function check_IfItemExists(ByVal item)", , "Error")
End Try
End Function
'*************END FUNCTIONS
'*************BUTTONS
' button: "Add Item"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'add item to data.xlsx, then reload_list from data.xlsx
Dim item As String
Dim itemquantity As Integer
Dim itemin As Integer
Dim itemout As Integer
Dim itemprice As Double
item = TextBox1.Text
itemquantity = NumericUpDown1.Value
itemin = NumericUpDown2.Value
itemout = NumericUpDown3.Value
itemprice = NumericUpDown4.Value
If check_IfItemExists(item) = True Then
MsgBox("This item already exists.", , "Error")
GoTo jump
End If
Try
If Not itemin + itemout = itemquantity Then
MsgBox("The amount of items in & out of stock do not total the whole quantity of items")
GoTo jump
End If
Catch ex As Exception
MsgBox("Could not add integers.", , "Error")
GoTo jump
End Try
Try
oBookInventory.Cells(itemcount + 3, 1).Value = item
oBookInventory.Cells(itemcount + 3, 2).Value = itemquantity
oBookInventory.Cells(itemcount + 3, 3).Value = itemin
oBookInventory.Cells(itemcount + 3, 4).Value = itemout
oBookInventory.Cells(itemcount + 3, 5).Value = itemprice
Catch ex As Exception
MsgBox("Could not write to data.xlsx", , "Error")
GoTo jump
End Try
update_ItemCountANDSave(+1)
reload_List()
TextBox1.Clear()
NumericUpDown1.Value = 0
NumericUpDown2.Value = 0
NumericUpDown3.Value = 0
jump:
End Sub
' button: "Remove Item"
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'remove item
If MsgBox("Are you sure you would like to delete this item?", MsgBoxStyle.YesNo, "Delete") = MsgBoxResult.Yes Then
Try
oBookInventory.rows(SIERow).delete()
Catch ex As Exception
MsgBox("Did not remove row from data.xls", , "Error: Button Remove Item")
End Try
update_ItemCountANDSave(-1)
reload_List()
End If
End Sub
' button: "Update Item" (Edit)
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
update_SIERow()
Dim item As String
Dim itemquantity As Integer
Dim itemin As Integer
Dim itemout As Integer
Dim itemprice As Double
item = TextBox2.Text
itemquantity = NumericUpDown5.Value
itemin = NumericUpDown6.Value
itemout = NumericUpDown7.Value
itemprice = NumericUpDown8.Value
Try
If Not itemin + itemout = itemquantity Then
MsgBox("To save the amount of items in & out of stock do not total the whole quantity of items", , "Error")
GoTo jump
End If
Catch ex As Exception
MsgBox("Could not add integers.", , "Error")
End Try
If MsgBox("Are you sure you would like to apply this update?", MsgBoxStyle.YesNo, "Check") = MsgBoxResult.Yes Then
Try
oBookInventory.Cells(SIERow, 1).Value = item
oBookInventory.Cells(SIERow, 2).Value = itemquantity
oBookInventory.Cells(SIERow, 3).Value = itemin
oBookInventory.Cells(SIERow, 4).Value = itemout
oBookInventory.Cells(SIERow, 5).Value = itemprice
update_ItemCountANDSave(0)
reload_List()
Catch ex As Exception
MsgBox("Could not write to data.xlsx", , "Error")
End Try
End If
jump:
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Define Excel variables
FileConfig = oExcel.Workbooks.Add(filelocation)
oBookConfig = FileConfig.Worksheets(1)
oBookInventory = FileConfig.Worksheets(2)
oBookComplaints = FileConfig.Worksheets(3)
'Define globally used variables
itemcount = oBookConfig.Cells(3, 2).Value()
complaintcount = oBookConfig.Cells(4, 2).Value()
Call reload_List()
oExcel.DisplayAlerts = False
'set up listveiw
ListView1.View = View.Details
ListView1.GridLines = False
ListView1.MultiSelect = False
ListView1.Sorting = SortOrder.Ascending
ListView1.Columns.Add("Items", 140, HorizontalAlignment.Left)
ListView1.Columns.Add("Quantity", 70, HorizontalAlignment.Center)
ListView1.Columns.Add("In", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("Out", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("Price", 70, HorizontalAlignment.Center)
jump:
End Sub
'*************END BUTTONS
' *** *** *** Under Construction *** *** ***
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
'Define Excel variables
FileConfig = oExcel.Workbooks.Add(filelocation)
oBookConfig = FileConfig.Worksheets(1)
oBookInventory = FileConfig.Worksheets(2)
oBookComplaints = FileConfig.Worksheets(3)
'Define globally used variables
itemcount = oBookConfig.Cells(3, 2).Value()
complaintcount = oBookConfig.Cells(4, 2).Value()
Call reload_List()
oExcel.DisplayAlerts = False
'set up listveiw
ListView1.View = View.Details
ListView1.GridLines = False
ListView1.MultiSelect = False
ListView1.Sorting = SortOrder.Ascending
ListView1.Columns.Add("Items", 140, HorizontalAlignment.Left)
ListView1.Columns.Add("Quantity", 70, HorizontalAlignment.Center)
ListView1.Columns.Add("In", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("Out", 50, HorizontalAlignment.Center)
ListView1.Columns.Add("Price", 70, HorizontalAlignment.Center)
jump:
End Sub
End Class