I actually had a slow day today at work and got a basic working model of what I wanted. I figured I'd post the code in case anyone else had a similar need. It's nothing special, just we are growing at about 45% per year, so the need for centralized info has become essential.
Code:
Option Explicit
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const SYNCHRONIZE = &H100000
Private Const INFINITE = &HFFFFFFFF
Private Type HelpDatabase
Programmer As String
Company As String
ProgramName As String
ProgramLocation As String
HelpFile As String
Delete As Boolean
End Type
Dim MasterArray(1 To 1000) As HelpDatabase ' I will most likely make this dynamic in and redim in future updates
Dim Index As Integer
Dim BadRead As Boolean
Private Sub ClearBoxes_Click()
Call Clear
End Sub
Private Sub cmdExit_Click()
Call WriteFile
Unload frmMain
End
End Sub
Private Sub cmdViewHelpClick()
Me.Visible = False
ProcessHandle Shell(("F:\UEdit\UEDIT32.EXE " & Trim(txtHelpFileLocation.Text)), vbNormalFocus)
Me.Visible = True
End Sub
Private Sub FormLoad()
Dim BadReadCount As Integer
BadRead = False
BadReadCount = 0
JobSelect.Enabled = True
Call ReadFile
While BadRead = True And BadReadCount < 10
DoEvents
Sleep 5000
Call ReadFile
BadReadCount = BadReadCount + 1
Wend
If BadReadCount > 9 Then
MsgBox ("Input database file has been locked for over 1 minute." & vbCrLf & "Please verify that F:\BRIAN\HelpFile_Database\Database.txt is not locked" & vbCrLf & "Program is exiting now...")
Unload frmMain
End
End If
frmMain.Height = "11475"
frmMain.Width = "9915"
cmdViewHelp.Enabled = False
cmdDeleteRecord.Enabled = False
cmdViewHelp.Enabled = False
End Sub
Private Sub ReadFile()
Index = 0
If Dir("F:\BRIAN\HelpFileDatabase\Database.txt") <> "" Then
If FileLocked("F:\BRIAN\HelpFileDatabase\Database.txt") Then
BadRead = True
Exit Sub
End If
Open "F:\BRIAN\HelpFileDatabase\Database.txt" For Input As #1
While Not EOF(1)
Index = Index + 1
Input #1, MasterArray(Index).Programmer, MasterArray(Index).Company, MasterArray(Index).ProgramName, MasterArray(Index).HelpFile, MasterArray(Index).ProgramLocation
JobSelect.AddItem MasterArray(Index).Company & " - " & MasterArray(Index).ProgramName
JobSelect.ItemData(JobSelect.NewIndex) = Index
Wend
Close #1
End If
End Sub
Private Sub WriteFile()
Dim i As Integer
Open "F:\BRIAN\HelpFileDatabase\Database.txt" For Output As #2
For i = 1 To Index
If MasterArray(i).Delete = False Then
Write #2, MasterArray(i).Programmer, MasterArray(i).Company, MasterArray(i).ProgramName, MasterArray(i).HelpFile, MasterArray(i).ProgramLocation
End If
Next i
Close #2
End Sub
Private Sub ProcessHandle(ProcessID As String)
Dim ProcessHandle
On Error GoTo 0
ProcessHandle = OpenProcess(SYNCHRONIZE, 0, ProcessID)
If ProcessHandle <> 0 Then
Call WaitForSingleObject(ProcessHandle, INFINITE)
Call CloseHandle(Process_Handle)
End If
End Sub
Function FileLocked(FileName As String) As Boolean
On Error Resume Next
Open FileName For Binary Access Read Write Lock Read Write As #1
Close #1
If Err.Number <> 0 Then
MsgBox ("Error #" & Str(Err.Number) & " - " & Err.Description & " when trying to open " & FileName & "!" & vbCrLf & "Please wait a moment and try again.")
FileLocked = True
Err.Clear
End If
End Function
Private Sub Form_Unload(Cancel As Integer)
Call WriteFile
Unload frmMain
End
End Sub
Private Sub JobSelect_Click()
If JobSelect.ListIndex <> -1 Then
cmdDeleteRecord.Enabled = True
cmdViewHelp.Enabled = True
txtProgrammer.Text = MasterArray(JobSelect.ItemData(JobSelect.ListIndex)).Programmer
txtCompany.Text = MasterArray(JobSelect.ItemData(JobSelect.ListIndex)).Company
txtProgramName.Text = MasterArray(JobSelect.ItemData(JobSelect.ListIndex)).ProgramName
txtHelpFileLocation.Text = MasterArray(JobSelect.ItemData(JobSelect.ListIndex)).HelpFile
txtProgramLocation.Text = MasterArray(JobSelect.ItemData(JobSelect.ListIndex)).ProgramLocation
End If
End Sub
Private Sub cmdAddRecord_Click()
If Trim(txtProgramName.Text) <> "" Then
JobSelect.AddItem txtCompany.Text & " - " & txtProgramName.Text
Index = Index + 1
MasterArray(Index).Programmer = txtProgrammer.Text
MasterArray(Index).Company = txtCompany.Text
MasterArray(Index).ProgramName = txtProgramName.Text
MasterArray(Index).HelpFile = txtHelpFileLocation.Text
MasterArray(Index).ProgramLocation = txtProgramLocation.Text
MasterArray(Index).Delete = False
JobSelect.ItemData(JobSelect.NewIndex) = Index
Call Clear
End If
End Sub
Private Sub Clear()
txtProgrammer.Text = ""
txtCompany.Text = ""
txtProgramName.Text = ""
txtHelpFileLocation.Text = ""
txtProgramLocation.Text = ""
End Sub
Private Sub cmdDeleteRecord_Click()
If JobSelect.ListIndex <> -1 Then
MasterArray(JobSelect.ItemData(JobSelect.ListIndex)).Delete = True
JobSelect.RemoveItem JobSelect.ListIndex
Call Clear
Index = Index - 1
End If
If JobSelect.ListIndex = -1 Then
cmdDeleteRecord.Enabled = False
cmdViewHelp.Enabled = False
End If
End Sub
Enter your message below
Sign in or Join us (it's free).