If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Excel > A macro to authorize a user to run another macro.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-11-10, 20:49
abhay_547 abhay_547 is offline
Registered User
 
Join Date: Sep 2009
Posts: 78
A macro to authorize a user to run another macro.

Hi All,

I have created an excel addin which contains some macros but i have sent the same to all my team members but i want to restrict some of the team members from using the same .i.e who are not authorized to use the above mentioned addin. So I want a macro which should first check the existence of a user's XP ID in my sql database table and then allow him to run the macro from my addin. If the user's XP ID is not present in my sql database then it should show a message that "You are not a authorized user to run this macro". I have a code with me which actually checks the existence of Windows XP user id in Excel workbook but I want a code which should check the existence of XP id in sql server table. Following is the code :
HTML Code:
Sub Test()
Dim ws As Worksheet
Dim strSQL As String
Dim strConnection_String As String
Dim x As String
Dim strFilePathOfAuthorizedUsersFile As String
'-----------------------------------------------------------
'NOTE: Requires reference to ADO library:
'     1. Open the Visual Basic Editor (Alt + Fll)
'     2. Choose Tools | References
'     3. Check box for Microsoft ActiveX Data Object 2.8 Library (or higher)
'-----------------------------------------------------------

'-----------------------------------------------------------
'ENTER YOUR SOURCE FILE WHERE NAMES ARE KEPT
'1) Must be an Excel file with One Column,
'2) Column header must be labeled: "NameOfAuthorizedUser"
'3) Data is a named range call "MyRange"
strFilePathOfAuthorizedUsersFile = "C:\MySourceTest.xls"
'-----------------------------------------------------------

    'SQL String
    strSQL = _
    "SELECT NameOfAuthorizedUser " _
    & "FROM myRange " _
    & "WHERE " _
    & "NameOfAuthorizedUser = '" & Environ("Username") & "';"
    
    'Connection String to get data from an Excel file
    strConnection_String = _
    "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & strFilePathOfAuthorizedUsersFile & ";" & _
        "Extended Properties=Excel 8.0;"
    
    x = CheckForAuthorizedUser(strConnection_String, strSQL)
    If x <> "" Then
        MsgBox "User " & x & " Found!"
    Else
        MsgBox "No Authorized User Found."
    End If
    
End Sub
Function CheckForAuthorizedUser(ByVal strConnection_String As String, ByVal strSQL As String) As String
'Creates a recordset from Excel, using filter criteria from the calling sub
'Returns a name or an empty string
'-----------------------------------------------------------
'NOTE: Requires reference to ADO library:
'     1. Open the Visual Basic Editor (Alt + Fll)
'     2. Choose Tools | References
'     3. Check box for Microsoft ActiveX Data Object 2.8 Library (or higher)
'-----------------------------------------------------------

Dim x As Long
Dim myRecordset As ADODB.Recordset
Set myRecordset = New ADODB.Recordset

    'sql string - uses module-level constants
    Debug.Print strSQL
    
    'initialize recordset and run the query
    Call myRecordset.Open(strSQL, strConnection_String, CursorTypeEnum.adOpenForwardOnly, _
        LockTypeEnum.adLockReadOnly, CommandTypeEnum.adCmdText)
    
    'Results
    If Not myRecordset.EOF Then
        CheckForAuthorizedUser = myRecordset.fields(0).Value
    Else
        CheckForAuthorizedUser = ""
    End If
    
    'Close recordset object and release memory
    If (myRecordset.State And ObjectStateEnum.adStateOpen) Then myRecordset.Close
    Set myRecordset = Nothing

End Function
Thanks for your help in advance.
Reply With Quote
  #2 (permalink)  
Old 04-12-10, 07:17
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
  #3 (permalink)  
Old 04-12-10, 15:06
abhay_547 abhay_547 is offline
Registered User
 
Join Date: Sep 2009
Posts: 78
A macro to authorize a user to run another macro.

Yes, I have posted it from my end over there as well so that I can get the answer asap.
Reply With Quote
  #4 (permalink)  
Old 04-12-10, 16:51
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Hi,

Yes, I understand why you have posted there too, and that's fair enough: you want the best answer as quickly as possible.

But from my point of view it's a little bit different:
I allocate x amount of time each day to freely help people on online forums such as this one. I don't want to spend y amount of time trying to answer a problem for someone when their question has already been answered elsewhere: I would rather spend that time working on providing an answer to a problem which will help someone! As such, I think it is polite to include cross-post links so people can easily check if the problem has been solved elsewhere.

Ken Puls sums this up really nicely here:
A message to forum cross posters | Excelguru.ca

Just something to bear in mind...
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
  #5 (permalink)  
Old 04-12-10, 23:20
abhay_547 abhay_547 is offline
Registered User
 
Join Date: Sep 2009
Posts: 78
A macro to authorize a user to run another macro.

Hi Colin,

That's true and that's why I follow a practice where in if I get answer of certain post in one of the forum then i post the answer on the other forum as well that it's answered now.
Reply With Quote
  #6 (permalink)  
Old 04-13-10, 04:02
Colin Legg Colin Legg is offline
Registered User
 
Join Date: Sep 2008
Location: London, UK
Posts: 495
Hi,
Quote:
That's true and that's why I follow a practice where in if I get answer of certain post in one of the forum then i post the answer on the other forum as well that it's answered now.
In my opinion that doesn't entirely cover the issue. I think the links should be added as soon the cross posts are created, and each forum you post in should be given some time (perhaps a day) to respond before you post somewhere else.

For example, on this one, you hadn't answered back to say it was solved and I was still trying to help you:
How to use different Face ID's in a Toolbar.

And in the meantime two people spent their time simultaneously answering your question:
How to assign different face id to macros in a toolbar - MrExcel Message Board
http://www.vbaexpress.com/forum/showthread.php?p=210431

Some people feel so strongly about this that some forums (not this one) have rules that x-posts must be added to the thread straight away. Anyway, this isn't worth getting bogged down on. It's just a suggestion....
__________________
Colin

RAD Excel Blog

Other tutorials:
Array Formulas | Deleting Rows with VBA
Reply With Quote
  #7 (permalink)  
Old 04-13-10, 15:38
abhay_547 abhay_547 is offline
Registered User
 
Join Date: Sep 2009
Posts: 78
A macro to authorize a user to run another macro.

Ok. I will Ensure this from next time.
Reply With Quote
  #8 (permalink)  
Old 04-14-10, 00:08
abhay_547 abhay_547 is offline
Registered User
 
Join Date: Sep 2009
Posts: 78
A macro to authorize a user to run another macro.

Hi,

Please expedite.....
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On