function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vstrausbvstrausb 

Problems logging onto Salesforce from MS Access form

I have a form in MS Access for input of username and password.  If the login is successful, another form is opened to retrieve a record from Salesforce.  I do not receive an error message that the login failed and the form2 does not open.
 
Here is the procedural code:
 
Option Compare Database
Option Explicit
'Declare an OfficeToolkit object.
'The WithEvents keyword enables asynchronous function calls
Dim WithEvents g_sfApi As SForceOfficeToolkitLib3.SForceSession3
Public Username As Object
Public Password As Object
 Public Function SFLogin(Username As Object, Password As Object) As Boolean
'setup for exception type error handling
On Error GoTo handleError
' create a session object
    Set g_sfApi = New SForceOfficeToolkitLib3.SForceSession3
' make a login call
    SFLogin = g_sfApi.Login(Username, Password)
 
If g_sfApi.Error <> NO_SF_ERROR Then
MsgBox g_sfApi.ErrorMessage
Exit Function
End If
Exit Function
handleError:
'look at the exception message
MsgBox Err.Description
'look at the message in the session
MsgBox g_sfApi.ErrorMessage
End Function

Private Sub Form_Load()
Set g_sfApi = New SForceOfficeToolkitLib3.SForceSession3
End Sub
Private Sub Login_to_Salesforce_Click()

If g_sfApi.IsLoggedIn Then
 DoCmd.Close
 DoCmd.OpenForm "Form_Form2"
Else
If g_sfApi.Error <> NO_SF_ERROR Then
MsgBox g_sfApi.ErrorMessage
End If
End If
End Sub
Private Sub SF_Password_LostFocus()
Set Password = SFPassword.Value
End Sub
Private Sub SF_User_Name_LostFocus()
Set Username = SFUser.Value
End Sub
Any ideas why form2 is not opening?  How can I determine if loggin is successful?
Thanks,
 - Vicki
RickNTARickNTA
It's not clear to me exactly what your design is, and I'm a bit rusty on SF login methods so haven't looked at that - but I notice your code is trying to open "Form_Form2".  While it's possible that's the actual form name, it's more likely the form name is "Form2" ("Form_Form2" is the name of the class module representing the form).  OTH, you should get an error message when Access can't find a form named Form_Form2 - so maybe the real problem is that the login is failing and your code never hits the DoCmd.OpenForm line.  I would step through the code in debug mode.  HTH!
CKACKA
Hi Vicki,
 
A couple things:
 
- You need to explicitly call your SFLogin function from the button click event
- You need to pass string values to the Login function
- Your call to open the second form should reference the name, "Form_2" in the code below
 
The following should work:
 
Option Compare Database
Option Explicit

'Changed to public to make available to other forms
Public WithEvents g_sfApi As SForceOfficeToolkitLib3.SForceSession3
'Changed to string
Public Function SFLogin(Username As String, Password As String) As Boolean

    On Error GoTo handleError
   
    Set g_sfApi = New SForceOfficeToolkitLib3.SForceSession3
        ' make a login call
        SFLogin = g_sfApi.Login(Username, Password)
 
    If g_sfApi.Error <> NO_SF_ERROR Then
        MsgBox g_sfApi.ErrorMessage
        Exit Function
    End If
   
    Exit Function
   
handleError:
    'look at the exception message
    MsgBox Err.Description
    'look at the message in the session
    MsgBox g_sfApi.ErrorMessage
   
End Function
 
 
Private Sub Login_to_Salesforce_Click()
   
    'Added call to login
 
    SFLogin SFUser.Value, SFPassword.Value
   
    If g_sfApi.IsLoggedIn Then
        'Changed close of form to hiding of form
        Me.Visible = False
        'Changed name of form being opened
        DoCmd.OpenForm "Form2"
    Else
        If g_sfApi.Error <> NO_SF_ERROR Then
            MsgBox g_sfApi.ErrorMessage
        End If
    End If
   
End Sub

In order to have your API object available to the second form you're launching, I've also change the instantiation of the session object to a public declaration and changed the closing of your login form to making it invisible since closing the form destroys the session object. You'll need to call it by referencing the form, then the session. For example, if your login form is called "Login", then you would reference the API session by calling Forms!Login.g_sfApi.
 
Depending on how many forms you're going to build into this, you may want to consider creating a base module to handle the session and make all calls to the API through that base module. This would get you around having to keep your login form open and hidden.
 
Hope this helps.
 
CKA
 
 
vstrausbvstrausb
Thanks CKA!  That worked!