Using Microsoft Access to Manage SQL Azure Database Users and Roles
Private Sub cmdCreateRole_Click()
Dim strSQL As String
If Len(Me.txtNewRole & vbNullString) = 0 Then
MsgBox "Please enter a name for the new Role.", vbCritical
Else
strSQL = "CREATE ROLE " & Me.txtNewRole
If ExecSQLAzureSQL(strSQL) = False Then
MsgBox "The Role was not created. Please try again.", vbCritical
Else
MsgBox "The Role """ & Me.txtNewRole & """ was created.", vbInformation
End If
End If
End Sub
Private Sub cmdAddUserToRole_Click()
Dim strRole As String
Dim strUser As String
If Len(Me.txtRoleName & vbNullString) = 0 Then
MsgBox "Please enter a name for the Role.", vbCritical
Else
strRole = Me.txtRoleName
If Len(Me.txtUserName & vbNullString) = 0 Then
MsgBox "Please enter a name for the User.", vbCritical
Else
strUser = Me.txtUserName
Call AddUserToSQLRole(strRole, strUser)
End If
End If
End Sub
Public Function ExecSQLAzureSQL(strSQL As String) As Boolean
On Error GoTo ErrHandle
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
ExecSQLAzureSQL = False
Set db = CurrentDb
Set qdf = db.CreateQueryDef("")
qdf.Connect = obfuscatedFunctionName("Wb_gR%/PD\-k&yZq~j>l")
qdf.SQL = strSQL
qdf.ReturnsRecords = False
qdf.Execute dbFailOnError
ExecSQLAzureSQL = True
ExitHere:
On Error Resume Next
Set qdf = Nothing
Set db = Nothing
Exit Function
ErrHandle:
MsgBox "Error " & Err.Number & vbCrLf & Err.Description _
& vbCrLf & "In procedure ExecSQLAzureSQL"
Resume ExitHere
End Function
Public Function obfuscatedFunctionName(strIn As String) As String
If strIn = "Wb_gR%/PD\-k&yZq~j>l" Then
obfuscatedFunctionName = "ODBC;" _
& "DRIVER={SQL Server Native Client 10.0};" _
& "SERVER=tcp:MyServerName.database.windows.net,1433;" _
& "UID=MyUserName@MyServerName;" _
& "PWD=MyPassword;" _
& "DATABASE=MySQLAzureDatabaseName;" _
& "Encrypt=Yes"
Else
obfuscatedFunctionName = vbNullString
End If
End Function
Function AddUserToSQLRole(strRole As String, strUser As String) As Boolean
Dim strSQL As String
AddUserToSQLRole = False
strSQL = "EXEC sp_addrolemember '" & strRole & "', '" & strUser & "';"
If ExecSQLAzureSQL(strSQL) = True Then
AddUserToSQLRole = True
MsgBox "User """ & strUser & """ was added to the """ _
& strRole & """ Role.", vbInformation
Else
MsgBox "User """ & strUser & """ was NOT added to the """ _
& strRole & """ Role." & vbCrLf & vbCrLf _
& Space(30) & "Please try again.", vbCritical
End If
End Function