Create Module

Gerrit.B

Board Regular
Joined
Aug 10, 2004
Messages
237
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
In my table if have fields like Carrier, BL and others.
In the table with carriers i have already created fields like URL1 and URL2.
All carriers have their own way of building a url for tracking purposes.
So i wanted to create a module so i can directly see the actual status of every shipment, in a AxtiveX Control (webbrowser)
For every carrier want to define a rule to build the url.

Who could help me to create a first start of the module, or tell me what i am doing wrong.


Option Compare Database
Option Explicit

Dim URL As String



Function Tracking_URL(Carrier As Byte, BL As String, URL1 As String, URL2 As String)
If Carrier = 58 Then ' Place here the carrier ID
URL = "URL1 & BL & URL2"
Else
End If
If Carrier = 62 Then ' Place here the carrier ID
URL = "URL1 & BL"
Else
URL = "Empty"
End Function


G.
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
You shouldn't have quotes around the concatenations.

Perhaps more importantly you should use the name of the function, which appears to be Tracking_URL instead of just URL.

If there are a lot of carriers then it's probably better to use Select Case.

That could also be helpful if more than one carrier uses the same rules.
Code:
Function Tracking_URL(Carrier As Byte, BL As String, URL1 As String, URL2 As String) As String
 
    Select Case Carrier
 
            Case 58
               Tracking_URL = URL1 & BL & URL2
            
            Case 62
               Tracking_URL = URL1 & BL
   
            Case Else
               Tracking_URL = "Empty
 
     End Select
 
End Function
When there are carriers with the same rules you can just add them to the existing Cases.

For example if carriers 45 and 76 have the same rules as carrier 58 you could use this.
Code:
Case 45, 58, 78
 
Upvote 0
Norie,

Tested this in sample database and it works great.

Thanks

G.
 
Upvote 0
I'm inclined to use an enum in these situations as I don't like remembering magic id numbers. Something like this will work in a module

Code:
Public Enum Carrier
  USPS = 1
  UPS = 58
  FedEx = 59
  SomeGuyOnABike = 60
End Enum
 
Function Tracking_URL(TheCarrier As Carrier)
...
  Select Case TheCarrier
    Case Carrier.UPS, Carrier.FedEx
    Case Carrier.USPS
    Case Carrier.SomeGuyOnABike
    Case Else
  End Select
...
End Function

hth,

Rich
 
Upvote 0
I started with code below (from Norie)

Option Compare Database
Option Explicit

Function Tracking_URL(Carrier As Byte, BL As String, URLP1 As String, URLP2 As String) As String
'If more use the same rules you can use Case 45, 58, 78
'Selected fields need to be filled, even if they are not needed. Or else it will give an ERROR
'
'
'
'
Select Case Carrier

Case 58 'Add here companyID
Tracking_URL = URLP1 & Mid(BL, 5, 9)

Case 55 'Add here companyID
Tracking_URL = URLP1 & Mid(BL, 5, 5) & URLP2

Case Else
Tracking_URL = "Empty"

End Select

End Function


Now i get an error when selecting empty fields, for carriers where these fields are NOT used at all.
Sample Carrier 1 uses only URL1 and part of BL so field URL2 is selected in module but not used. How can i avoid all these errors.

G.
 
Upvote 0
How exactly are you using the formula?

What errors are you getting?
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,337
Members
452,907
Latest member
Roland Deschain

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top