Option Explicit
Option Compare Database
#If VBA7 Then ' Access 2010 or later
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal Milliseconds As LongPtr)
#Else ' Access 2007 or earlier
Public Declare Sub Sleep Lib "kernel32" (ByVal Milliseconds As Long)
#End If
Public Function QBOTest()
' Our .NET interface
Dim qbo As QBOInterface.QBOInterface
Set qbo = New QBOInterface.QBOInterface
Dim authCode As String, realmID As String
' Authorization Code, one-time code from QBO that authorizes our application to read data from customer's QBO account
authCode = DLookup("AuthCode", "QBOAuthentication") ' will need to be entered by the user
' Unique identifier for the client's company in QBO
realmID = DLookup("RealmID", "QBOAuthentication")
' Init: clientID, clientSecret, redirectURL, environment, authCode, realmID
' These must match exactly what the web application uses
' ClientID and ClientSecret come from QBO Developer Account portal
qbo.Init "Q0uEmaJmYxjLE73PffMaG0EhIhG7**************", "WyBAFRAoLjGwXF27zjl2OUnBjwA**************", "https://accessqbotest.azurewebsites.net/callback", "sandbox", authCode, realmID
Dim aToken As String, rToken As String
' Access Token, used to communicate with the QBO API, expires within 60 minutes
aToken = "" ' will come from the database, otherwise will be blank
' Refresh Token, used to request Access Tokens, expires after 100 days
rToken = DLookup("RefreshToken", "QBOAuthentication") ' will come from the database, if QBO connectivity has occurred before, otherwise this will be blank
' Gets our Access Token, if we don't already have one to use
qbo.GetToken aToken, rToken
Sleep 2000 ' Short delay is required for the API to be called and response received/processed
' MsgBox "Access Token: " & qbo.AccessToken
' MsgBox "Refresh Token: " & qbo.refreshToken
Dim companyInfo As String
companyInfo = qbo.GetCompanyInfo
' MsgBox "Company Info (JSON): " & companyInfo
Dim Json As Object
Set Json = JsonConverter.ParseJson(companyInfo)
Dim sql As String
Dim dbs As DAO.Database
Set dbs = CurrentDb()
RunSQL "DELETE FROM QBOCompany", dbs
RunSQL "INSERT INTO QBOCompany (ID, CompanyName, CompanyAddr_City, CompanyAddr_Country, CompanyAddr_Line1, CompanyAddr_PostalCode, PrimaryPhone, EmailAddress) VALUES ('" & Json("Id") & "', '" & Json("CompanyName") & "', '" & Json("CompanyAddr")("City") & "', '" & Json("CompanyAddr")("Country") & "', '" & Json("CompanyAddr")("Line1") & "', '" & Json("CompanyAddr")("PostalCode") & "', '" & Json("PrimaryPhone")("FreeFormNumber") & "', '" & Json("Email")("Address") & "')", dbs
dbs.Close
Set dbs = Nothing
MsgBox "Sync complete!"
End Function
Private Sub RunSQL(sql As String, dbs As DAO.Database)
dbs.Execute sql, dbFailOnError
End Sub