Class: Bitly in Category Internet/Web : Internet/Web from Total Visual SourceBook

Use the Bit.ly web service to convert a long Internet URL string to a short one and vice versa with VBA and VB6.

Bit.ly is used by Twitter to shorten URLs in tweets. For your own use, a single Bit.ly access token can be used for OAuth authentication. To use for free, head to https://bitly.com/a/oauth_apps.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CBitly class.
AccessToken Property Bit.ly Access token which is for personal use of Bit.ly API calls.
UserName Property Bit.ly user name for multiple account use of Bit.ly API calls.
ApiKey Property Bit.ly API Key multiple account use of Bit.ly API calls.
Class_Initialize Initialize Initialize values to defaults which may be overridden with property settings and methods.
GetCredentials Private Create the syntax to provide the credentials to the Bit.ly. API callCredentials can be provided as an Access Token or User Name and API Key.
ShortenURL Method Create a shortened URL using the Bit.ly URL shortening service.
OriginalURL Method Convert a shortened Bit.ly URL to its original long URL.
InfoXML Method Get information for a page from a shortened Bit.ly URL. The result is in XML which can be parsed using the CXMLRead class.
URLEncode Private Encodes a string so it can be passed to a URL.
TrimResponse Private Process response to terminate at the first return character. If there isn't one, the entire string is returned.
' Examples of using the CBitly class to use the Bit.ly web service to convert a long Internet URL string to a short one and vice versa with VBA and VB6.
' To use Bit.ly API calls, you need to create an Bit.ly account (free) and get the OAuth authentication access token or user name and API key.
' See the declarations section of the CBitly class for additional details.
'
' To use this example, create a new module and paste this code into it.
' Then run the procedure by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

' Set these login values once you get them from bit.ly
Private Const mcstrAccessToken As String = ""
Private Const mcstrUserName As String = ""
Private Const mcstrAPIKey As String = ""

' Set the default URL to convert
Private Const mcstrLongURL As String = "http://www.fmsinc.com/MicrosoftAccess/SourceCodeLibrary.html"

Private Sub Example_CBitly()
  ' Commments: Examples of using the CBitly class to use the Bit.ly web service to convert a long Internet URL string to a short one and vice versa.

  Dim fOK As Boolean
  Dim clsBitly As CBitly
  Dim strLongURL As String
  Dim strBitlyURL As String
  Dim strOriginal As String
  Dim strInfoXML As String

  Set clsBitly = New CBitly

  ' Set login properties. Either the AccessToken alone or the user name/API Key combination is used.
  fOK = True
  If mcstrAccessToken <> "" Then
    clsBitly.AccessToken = mcstrAccessToken
  ElseIf (mcstrUserName <> "") And (mcstrAPIKey <> "") Then
    clsBitly.UserName = mcstrUserName
    clsBitly.ApiKey = mcstrAPIKey
  Else
    MsgBox "Set the Bit.ly login information in the Declarations section before running this code."
    fOK = False
  End If

  If fOK Then
    ' Prompt the user for a URL to shorten
    strLongURL = InputBox("Specify the URL to convert to Bit.ly", "CBitly Example", mcstrLongURL)

    If strLongURL <> "" Then
      ' Get the shortened URL from Bit.ly
      strBitlyURL = clsBitly.ShortenURL(strLongURL)
      If strBitlyURL <> "" Then
        Call InputBox(strLongURL & " converted to ", "BitlyURL Example", strBitlyURL)

        ' Show how to convert the Bit.ly shortened URL back to its original URL
        strOriginal = clsBitly.OriginalURL(strBitlyURL)
        If strOriginal = "" Then
          MsgBox "Shortened Bit.ly URL " & strBitlyURL & "cannot be converted back to its original URL. This is most likely because it wasn't saved to your account whent the bit.ly was created."
        Else
          MsgBox "Converted the shortened Bit.ly URL from " & strBitlyURL & " back to " & strOriginal
        End If

        strInfoXML = clsBitly.InfoXML(strBitlyURL)
        Debug.Print "Information on the URL in XML format:"
        Debug.Print strInfoXML
      End If
    End If
  End If

  ' Clean up
  Set clsBitly = Nothing
End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS