passwor protecting command buttons in VB

Status
Not open for further replies.

geekgod

Journeyman
i'm want to create a program in vb which will allow two sorts of entry..as a regular user and as a privilaged user.to enter as i privilaged user, u will hv to supply a password. the program i'm developing is a data driven program and the only advantage that a privilaged user gets is the ability to modify the database.
i was thinking of making two forms..the entry form with two radio buttons for the two types of users.selecting the privilaged user wil prompt for the password which when entered correctly will display the actual form with all the command buttons enabled.if the password is wrong or the normal user is selected..the buttons that are for modifying the database will be invisible.
i have made the second,ie the data access form..complete with the command buttons and everything.but i'm getting muddled with the logging in part.
it would be very helpful if some of u geeks could help me with the code of that part.
thanks...
 

siriusb

Cyborg Agent
Since you are having only two types of users (root and normal), declare a public boolean named root in the second form. If the login form validates the user as a root, then make it to set this public variable to true and then unload the login form. In the second form's load event, make visible/invisible the controls based on this root boolean.
Is this what you were in doubt?
 
OP
G

geekgod

Journeyman
ya..something like that..
now i am facing another problem
i created two forms before the original form..a selectuser for with two radio buttons for selecting the user types, and a login form to validate the administrator.
the selectuser form had the following code
Code:
Private Sub cmdok_Click()

If op1.Value = True Then
    usertype = False    'select normal user
ElseIf opt2.Value = True Then
    Call Load(frmLogin)
    Call frmLogin.Show
    If LoginSucceeded = True Then
    usertype = True
    Else
    usertype = False
    End If
End If
Call Load(frmview)
Call frmview.Show
Unload Me
End Sub
here's the screenshot
*www.iup.in/show.php/169_frmselect.JPG

the loginform was programmed thus
Code:
Private Sub cmdCancel_Click()
        LoginSucceeded = False
    Me.Hide
    
End Sub

Private Sub cmdok_Click()
   
    If txtPassword = "password" Then
       LoginSucceeded = True
        Me.Hide
    Else
        MsgBox "Invalid Password, try again!", , "Login"
        txtPassword.SetFocus
        SendKeys "{Home}+{End}"
    End If
End Sub

Private Sub Form_Load()

End Sub
i declared the two public variables LoginSucceeded and usertype as boolean in a separate module

finally, i included the following snippet in the needed portions of the main fom
Code:
Public Sub checkuser()
If usertype = False Then
Command2(4).Visible = False
Command2(5).Visible = False
Command2(6).Visible = False
Command2(7).Visible = False
End If
End Sub

now th problem is..whenever i run the program without the option of logging in,ie i directly go into the program as a particular type of user without validation,it is running fine.
but when i include the option of logging in..whichever type of user i select,the program always starts in the normal user mode..
what do u think is the problem?


----------------------------------------------------------------------------
 
OP
G

geekgod

Journeyman
there is another problem...
the program i created requires me to search from a database and display the results.for this i added a separate find for where i included an API call to display the available records as i type.
heres the code....

this is the API call..
Code:
Public Declare Function sendMessageByStrings& Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
                      ByVal lParam As String)
Public Const LB_SELECTSTRING = &H18C
Public gFindString As String
[quote]Public Const gDataBaseName = "C:\Documents and Settings\Apollo\My Documents\New Folder (2)\db12.mdb"

this is the code for the find form
Code:
Private Sub Form_Activate()
List1.Enabled = False
dtafind.DatabaseName = gDataBaseName
dtafind.Refresh
If (dtafind.Recordset.RecordCount > 0) Then
    Screen.MousePointer = vbHourglass
    dtafind.Recordset.MoveFirst
    While Not dtafind.Recordset.EOF
        List1.AddItem dtafind.Recordset.Fields(0) & ""
        dtafind.Recordset.MoveNext
    Wend
    List1.Enabled = True
    DoEvents
End If
lblcount = "There are " & dtafind.Recordset.RecordCount & " records"
Screen.MousePointer = vbDefault

End Sub

Public Property Let recordsource(ByVal sNewValue As String)
dtafind.recordsource = sNewValue
End Property

Private Sub Form_Unload(Cancel As Integer)
Set frmfind = Nothing
End Sub

Private Sub List1_DblClick()
gFindString = List1
Unload frmfind

End Sub

Private Sub txtfind_Change()
Dim entryNum As Long
Dim txttofind As String
txttofind = txtfind.Text
entryNum = sendMessageByStrings(List1.hwnd, LB_SELECTSTRING, 0, txttofind)

End Sub

and this is the code for linking the find form with the main program..this code is the event of clicking the "Find" button

Code:
Case cmdfind
Dim ireturn As Integer
gFindString = ""
With frmfind
    .recordsource = "SELECT term FROM Table1 ORDER BY term"
    .Show vbModal
End With
If (Len(gFindString) > 0) Then
    With Data1.Recordset
        .FindFirst "term = '" & gFindString & "' "
            If (.NoMatch) Then
                ireturn = MsgBox("term " & gFindString & _
                                 " was not found.", vbCritical, "term")
            Else
                ireturn = MsgBox("term " & gFindString & " was retrieved.", _
                              vbInformation, "term")
            End If
    End With
End If

now the problem is..when i run the program from within vb..it runs fine..
but when i make the .exe and run it..the search part does not work.the API call and the find form responds just fine..it even says "the ' ' term was retrieved" as i had instructed i to say..but the record that is returned by the find form is not displayed in the main form


another questoin...how do i make the loacation of the database as indicated in the code independant of where the setup file install the program..i mean..here i have given the location of gDataBasename as
Public Const gDataBaseName = "C:\Documents and Settings\Apollo\My Documents\New Folder (2)\db12.mdb"
but the user may not install the program in that manner...in fact there is little chance he will.so how what code do i need to include or modify to make this program run?

i know this was a very long post..thanks for bearing with it..
waiting for ur replies..
 

siriusb

Cyborg Agent
For your first problem, you need to call the login form (where you enter the password) like so:
Code:
Call frmLogin.Show(1, Me)
THis will make the if statement to wait till the login form is dismissed before rushing on to execute the next statements. That's the difference between modal and modeless forms.
 
OP
G

geekgod

Journeyman
thnks frnd..one of my problems solved now.. :)
waiting for the other to be solved too...

nd just for curiosity..@siriusb..how long have you been programming with vb?
 

siriusb

Cyborg Agent
I have no idea of the second problem. Hope you were able to solve it by now.
And storing absolute paths is a bad practice. You should always try to use relative paths or dynamic ones (like $windir or something). You cannot know where an mdb database would be stored. Your best bet is to search for commonly used paths (c:\, d:\, ...) or ask the user to "setup" your program or use a DSN.

I learnt most of the stuff when I was in 12th std. Now I don't use VB at all.
 
Status
Not open for further replies.
Top Bottom