I am trying to implement code associated with clicking the reset button in the administrative module of my application such that once a user has already been granted administrative access at the time the user was added to the excel database, and said username and password associated with the user are requested for and verified when the reset button is clicked, the application replaces the old username and password respectively of the said user in the excel database when that information is requested after verification. The relevant code block is as below;
Dim processes() As Process = Process.GetProcesses
For p As Integer = processes.Count - 1 To 0 Step -1
If processes(p).ProcessName = "EXCEL" Then
processes(p).Kill()
End If
Next
oExcel = CreateObject("Excel.Application")
Dim Path As String = "C:\Users\Vince\Desktop\Research\Current Research\Automating the GSRS\GSRS
with horizontal curve v 1.0\bin\Debug\Usernames.xlsx"
oBook = oExcel.Workbooks.Open(Path)
oSheet = oBook.Worksheets(1)
oSheet.activate()
sheetCount = oSheet.Cells(oSheet.Rows.Count, "A").End(XlDirection.xlUp).row
initialusername = "admin"
initialpassword = "password"
adminusername = InputBox("Please Enter admin username", "Alert")
adminpassword = InputBox("Please Enter admin password", "Alert")
usernameverify = InputBox("Please Enter new admin username", "Alert")
passwordverify = InputBox("Please Enter new admin password", "Alert")
Do While String.IsNullOrEmpty(adminusername) = True And String.IsNullOrEmpty(adminpassword) =
True
MsgBox("You are not authorized to reset the login credentials!",, "Alert")
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
Loop
Do While String.IsNullOrEmpty(adminusername) = False And String.IsNullOrEmpty(adminpassword) =
False
For j = 2 To sheetCount
ReDim Preserve usernames(j)
ReDim Preserve passwords(j)
If oSheet.Cells(j, 5).Text = "Administrator" Then
usernames(j) = oSheet.Cells(j, 3).Value
passwords(j) = oSheet.Cells(j, 4).Value
End If
Next
For j = 2 To sheetCount
If oSheet.Cells(j, 5).Text = "Administrator" Then
If (adminusername = initialusername Or adminusername = usernames(j)) And
(adminpassword = initialpassword Or adminpassword = passwords(j)) Then
oSheet.Cells(j, 3).Value = usernameverify
oSheet.Cells(j, 4).Value = passwordverify
Dim filefinal As IO.FileInfo =
My.Computer.FileSystem.GetFileInfo("Usernames.xlsx")
filefinal.IsReadOnly = False
oSheet.SaveAs(Path)
MsgBox("Administrator credentials have been updated",, "Success")
oExcel.Workbooks.Close()
oExcel.Quit()
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
ElseIf (adminusername <> usernames(j)) And (adminpassword <> passwords(j)) Then
MsgBox("You are Not authorized To reset the login credentials!",, "Alert")
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
ElseIf (adminusername = usernames(j)) And (adminpassword <> passwords(j)) Then
MsgBox("You are Not authorized To reset the login credentials!",, "Alert")
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
ElseIf (adminusername <> usernames(j)) And (adminpassword = passwords(j)) Then
MsgBox("You are Not authorized To reset the login credentials!",, "Alert")
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
End If
End If
Next
For j = 2 To sheetCount
ReDim Preserve usernames(j)
ReDim Preserve passwords(j)
If oSheet.Cells(j, 5).Text = "Regular User" Then
usernames(j) = oSheet.Cells(j, 3).Value
passwords(j) = oSheet.Cells(j, 4).Value
End If
Next
For j = 2 To sheetCount
If oSheet.Cells(j, 5).Text = "Regular User" Then
If (adminusername = initialusername Or adminusername = usernames(j)) And
(adminpassword = initialpassword Or adminpassword Is passwords(j)) Then
MsgBox("You are Not authorized To reset the login credentials!",, "Alert")
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
ElseIf (adminusername <> usernames(j)) And (adminpassword <> passwords(j)) Then
MsgBox("You are Not authorized To reset the login credentials!",, "Alert")
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
ElseIf (adminusername = usernames(j)) And (adminpassword <> passwords(j)) Then
MsgBox("You are Not authorized To reset the login credentials!",, "Alert")
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
ElseIf (adminusername <> usernames(j)) And (adminpassword = passwords(j)) Then
MsgBox("You are Not authorized To reset the login credentials!",, "Alert")
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
End If
End If
Next
Loop
Do While String.IsNullOrEmpty(adminusername) = True And String.IsNullOrEmpty(adminpassword) =
False
MsgBox("You are Not authorized To reset the login credentials!",, "Alert")
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
Loop
Do While String.IsNullOrEmpty(adminusername) = False And String.IsNullOrEmpty(adminpassword)
= True
MsgBox("You are Not authorized To reset the login credentials!",, "Alert")
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
Loop
Do While String.IsNullOrEmpty(adminusername) = True And String.IsNullOrEmpty(adminpassword) =
False
MsgBox("You are Not authorized To reset the login credentials!",, "Alert")
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
Loop
Do While String.IsNullOrEmpty(adminusername) = False And String.IsNullOrEmpty(adminpassword) =
True
MsgBox("You are Not authorized To reset the login credentials!",, "Alert")
Me.txtusername.Text = ""
Me.txtpassword.Text = ""
Exit Sub
Loop
After tinkering with it for a while, I believe the culprit is this code block;
For j = 2 To sheetCount
If oSheet.Cells(j, 5).Text = "Administrator" Then
If (adminusername = initialusername Or adminusername = usernames(j)) And
(adminpassword = initialpassword Or adminpassword = passwords(j)) Then
oSheet.Cells(j, 3).Value = usernameverify
oSheet.Cells(j, 4).Value = passwordverify.......
Next"
For some reason, as it loops, it doesn't discriminate on values of j based on my conditional
If oSheet.Cells(j, 5).Text = "Administrator" Then.....It simply continues from j= 2 to sheetcount in +1 increments. How do I go around this? Anyone with the experience and expertise to develop a workaround? Thanks.