Open a certain form dependant on textbox contents

SantasLittleHelper

Board Regular
Joined
Nov 25, 2016
Messages
77
I have created a Log-in form for staff to enter their username and password which is saved in a table. Is it possible that when they click log-in, it opens a certain form dependant on the username entered?
So let's say my username is 'j.smith' and I click Log-in.. I will be taken to the form called 'Joe Smith Homepage'. If I enter the username 'a.johnson' and I click Log-in.. I will be taken to the form called 'Adam Johnson Homepage'
Not sure how do do this, any help would be greatly appreciated.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
You should use a single form, but create a Property Let to change the name. This way, when you click login, you instantiate the form, update the title, and then show it.

For example, I created a UserForm called frmHomepage. On the form, I added a label called lblTitle. In the code module of the form, I added this code:

Code:
Property Let Title(sTitle As String)
  Me.lblTitle.Caption = sTitle
End Property


Property Get Title() As String
  Title = Me.lblTitle.Caption
End Property

For purposes of an example, I have Sheet1 where the user name will be in cell A1 before the macro runs. Then, in a standard module, I added this procedure. In your situation, you would incorporate some semblance of this code towards the end of your Login_Click() event:

Code:
Public Sub LoadHomepage()
  Dim myHome As frmHomepage
  
  Set myHome = New frmHomepage
  myHome.Title = Sheet1.Range("A1").Value & " Homepage"

  myHome.Show
End Sub
 
Upvote 0
I can't think of a way to do this in VBA, but it is probably possible using some Windows API calls.

Either way, I would not recommend it. ActiveX forms tend to be resource hogs, which may result in overly large workbooks and susceptible to corruption. Also, you would potentially be creating a maintenance nightmare - if you have nearly identical form for all users, and have to make a change, imagine doing it for 20 forms (=20 users) as opposed to just one.

Now, if you have certain user classes (for example, Joe Smith may be a regular user, whereas Adam Johnson may be an administrator), then you have options. You can use an approach similar to my previous example to show/hide (or enable/disable) certain controls based on user class, or you could call up an altogether different form for each class. The latter you could accomplish using some If or Select Case statements, a la:

Code:
Select Case userClass
  Case "admin": LoadAdminForm
  Case "readonly": LoadReadonlyForm
  Case Else: LoadStandardForm
End Select

Again, if you can get away with using just one form, I would recommend to go that route. I would only take the second approach if the forms for the various user classes are substantially different.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,213
Members
448,554
Latest member
Gleisner2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top