I have written an assembly in C# which returns a string array, the C# code is below:
[ComVisible(true)]
public class PostcodeFinder
{
public string[] SearchPostcodes(string postCode)
{
var searchService = new QuickAddress("http://x.x.x.x:xxxx/")
{Engine = QuickAddress.EngineTypes.Singleline, Flatten = true};
var mPicklist = searchService.Search("GBR", postCode, PromptSet.Types.OneLine);
var x = mPicklist.Picklist.Items.Count();
var resultsToReturn = new string[x];
for (var i = 0; i < x; i++)
{
resultsToReturn[i] = mPicklist.Picklist.Items[i].PartialAddress;
}
return resultsToReturn;
}
}
I have then built this assembly, being sure to tick the Register for COM interop
box in properties. Then in Microsoft Access, I have added the .tlb
file to the references and created a form with a couple of controls (one of which is a Listbox
control named lstResults
). Under the main button control, I have the following VBA code:
Private Sub btnSearch_Click()
Dim postcodeToSearch As String
postcodeToSearch = Me.txtPostcode
Dim c As New PostcodeFinder
Dim results
results = c.SearchPostcodes(postcodeToSearch)
End Sub
Edit: This runs without error, however when I query the Immediate window with ?results
after putting some dummy code below to allow me to place a breakpoint, I get the following error:
Run-time error '13' - Type mismatch
Effectively I want to rewrite the following C# code in VBA:
var results = c.SearchPostcodes(postcodeToSearch);
foreach(var x in results)
{
lstResults.Items.Add(x);
}
Thanks in advance