UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> URLEncode    

Sometime you need to create URL that contain characters that might not be always correctly interpreted. For example if an URL contains "{" and "}", then it needs to be escaped as "%7B" and "%7D", respectively. This is also useful for cases where you need to pass Unicode characters to functions that cannot handle non latin characters, such as Access' Application.FollowHyperLink for example.

A sample usage would be:

CODE
Application.FollowHyperLink "https://www.google.gr/maps/place/" & UrlEncode(strMyFavoriteGreekPlace)

Where the variable strMyFavoriteGreekPlace might be something like "Αθήνα, Ελλάδα". Note that you cannot hard-code the non-latin characters directly inside VBA source code nor the Immediate Windows but you can put it in Access controls or in tables and then read it into a VBA string variable.

CODE
' Code courtesy of UtterAccess Wiki
' http://www.utteraccess.com/wiki/index.php/Category:FunctionLibrary
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' REV  DATE                          DESCRIPTION
' 1.0  2014-08-28                  initial release

Private Const CP_UTF8 = 65001

Private Declare PtrSafe Function WideCharToMultiByte Lib "Kernel32" ( _
   ByVal CodePage As Long, _
   ByVal dwflags As Long, _
   ByVal lpWideCharStr As Long, _
   ByVal cchWideChar As Long, _
   ByVal lpMultiByteStr As Long, _
   ByVal cchMultiByte As Long, _
   ByVal lpDefaultChar As Long, _
   ByVal lpUsedDefaultChar As Long _
) As Long

Private Function UTF16To8(ByVal UTF16 As String) As String
   Dim sBuffer As String
   Dim lLength As Long
   
   On Error GoTo ErrHandler

   If UTF16 <> vbNullString Then
       lLength = WideCharToMultiByte(CP_UTF8, 0, StrPtr(UTF16), -1, 0, 0, 0, 0)
       sBuffer = Space$(lLength)
       lLength = WideCharToMultiByte(CP_UTF8, 0, StrPtr(UTF16), -1, StrPtr(sBuffer), Len(sBuffer), 0, 0)
       s
Buffer = StrConv(sBuffer, vbUnicode)
       UTF16To8 = Left$(sBuffer, lLength - 1)
   Else
       UTF16To8 = vbNullString
   End If

ExitProc:
   On Error Resume Next
   Exit Function
ErrHandler:
   Select Case Err.Number
       Case Else
           VBA.MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
   End Select
   Resume ExitProc
   Resume 'for Debugging
End Function

Public Function URLEncode( _
  StringVal As String, _
  Optional SpaceAsPlus As Boolean = False, _
  Optional UTF8Encode As Boolean = True _
) As String
   Dim StringValCopy As String
   Dim StringLen As Long

   On Error GoTo ErrHandler

   StringValCopy = IIf(UTF8Encode, UTF16To8(StringVal), StringVal)
   StringLen = Len(StringValCopy)
   
   If StringLen > 0 Then
       ReDim Result(StringLen) As String
       Dim I As Long, CharCode As Integer
       Dim Char As String, Space As String
   
     If SpaceAsPlus Then Space = "+" Else Space = "%20"
   
     For I = 1 To StringLen
       Char = Mid$(StringValCopy, I, 1)
       CharCode = Asc(Char)
       Select Case CharCode
         Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
           Result(I) = Char
         Case 32
           Result(I) = Space
         Case 0 To 15
           Result(I) = "%0" & Hex(CharCode)
         Case Else
           Result(I) = "%" & Hex(CharCode)
       End Select
     Next I
     URLEncode = Join(Result, "")
   End If

ExitProc:
   On Error Resume Next
   Exit Function
ErrHandler:
   Select Case Err.Number
       Case Else
           VBA.MsgBox "Error " & Err.Number & " (" & Err.Description & ")"
   End Select
   Resume ExitProc
   Resume 'for Debugging
End Function


Creative Commons License
URLEncode by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 2,612 times.  This page was last modified 01:19, 29 August 2014 by BananaRepublic.   Disclaimers