+ Reply to Thread
Results 1 to 10 of 10

Declaring Classes for UserForm - advance VBA

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Declaring Classes for UserForm - advance VBA

    Hi People,

    I'm trying to add properties to my userform1.

    I have function declared in class module:

    Function Login() As String
    Login = Environ("USERNAME")
    End Function

    And i want to have a property from userform1 object inside userform1 for each private sub (userform_activate, listbox_change etc.).

    How can I do this?

    I would like to have property like this (and when I have userform2 - i want to have this property also here if it is possible).
    Private Sub UserForm_Activate()
    
    dim Name as string 
    dim LastRow as long 
    
    LastRow  = A_Wniosek.Range("A1").CurrentRegion.Rows.Count
    
    Name = userform1.login() & "_" & LastRow
    
    end sub
    Thank You in advance for your help,
    Best regards,
    Jacek Antek

  2. #2
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Declaring Classes for UserForm - advance VBA

    Has anyone had similar problem?

    Best Regards,
    Jacek Antek

  3. #3
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Declaring Classes for UserForm - advance VBA

    Hi,

    What problem are you facing with adding a property to your userform? A userform is itself a class and therefore adding a property to it is no different to adding a property to any other class.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  4. #4
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Declaring Classes for UserForm - advance VBA

    Hi,

    The problem is that I have to everywhere in my code pass my Login Function to get current login.

    In my code i am declaring class:

    Dim Moja As New MyForm
    
    UserForm1.Login = Moja.Login()
    In class module i have function.

    Function Login() As Variant
    Login = Environ("USERNAME")
    End Function
    First question is : why in my userform1 I have to declare Public login as string ?
    I thought when you have your own propety declared to userform it will be enough.

    Second question:
    I want to have public Login variable in my whole project - in module, in userform etc. Now I have to call this function now in module (in userform it is working), but i have to do this second time in
    my called Sub Routine again, for example i can use function to get this variable once again. I want to have it declared only once for all modules in my project.
    It is possible ?

    Please help,
    Jacek Antek

  5. #5
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Declaring Classes for UserForm - advance VBA

    I fear you have not posted sufficient code to answer your first question. Nowhere have you indicated how or where you have implemented any property.

    For your second question I can see no good reason not to use a function for this. You may declare a public variable to hold the value but you will still have to call the function initially. You might simply rewrite the function to use a Static variable and return that if it is not empty.

  6. #6
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Declaring Classes for UserForm - advance VBA

    Quote Originally Posted by xlnitwit View Post
    I fear you have not posted sufficient code to answer your first question. Nowhere have you indicated how or where you have implemented any property.
    Ok, this is improvement:

    Module:

    Sub Wniosek()
    
    Dim Moja As New MyForm
    
    UserForm1.Login = Moja.Login()
    
    UserForm1.Show
    Class MyForm:

    Function Login() As Variant
    Login = Environ("USERNAME")
    End Function

    Userform1:

    Public Login As String
    Private Sub UserForm_Activate()
    
    Dim W As Long
    Dim numer As String
    
    W = A_Wniosek.Range("A1").CurrentRegion.Rows.Count
    numer = Me.Login & "_" & Format(W, "000000")
    
    Label_Numer_zgloszenia = numer
    
    Call Send_Mail
    
    End Sub
    So 2 problems:

    Do once time function Login to pass it to Userform and to other Sub (to Send_Email Sub for example) in other modules.

    What do you mean by rewriting function to do static variable ? What is it ?

    Best Regards,
    Jacek Antek

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Declaring Classes for UserForm - advance VBA

    Why not just use a module for your login routine? It acts as a static class to all intents and purposes

  8. #8
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Declaring Classes for UserForm - advance VBA

    Thank You Kyle123,

    In module (start _module)I start my code and define property login from class to userfom, next im going through userforms and last action is calling Sub_email from userfrom. I dont know why but my macro do not return to first module (start_module) when my userform is unloaded... If there will be a chance to return from Userform to start module i could do Public variable Login which should be working in calling sub routines ?

    Best Regards,
    Jacek Antek

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: Declaring Classes for UserForm - advance VBA

    Sorry, I really don't understand any of that - if you want some help, please post all your code - not just snippits

    Also an example workbook would be much better
    Last edited by Kyle123; 08-12-2016 at 05:49 AM.

  10. #10
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Poland
    MS-Off Ver
    Excel 2016-365
    Posts
    2,946

    Re: Declaring Classes for UserForm - advance VBA

    Ok,

    no problem

    I have attached my macro.

    Best Regards,
    Jacek Antek
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Automatic advance between textboxes on a userform
    By scott micklo in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-22-2016, 06:21 PM
  2. T-value for different classes
    By dr.cuco in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 12-14-2013, 07:16 PM
  3. Replies: 5
    Last Post: 11-04-2013, 11:28 AM
  4. UserForm Assign Roll Number to Different Classes
    By z-eighty2 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-10-2013, 10:00 PM
  5. Declaring Public Variables to create a userform
    By samz93 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-02-2013, 08:47 AM
  6. Declaring specific userform variables/checkbox
    By HOT97ECLIPSEGSX in forum Excel - New Users/Basics
    Replies: 15
    Last Post: 06-21-2010, 03:09 PM
  7. Classes
    By blatham in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-20-2008, 05:47 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1