Archive for August 2013

HTTP post in VBA

I have some very nice C# .NET web services that I created and use from some web sites, .NET desktop applications, and iOS apps. But of course, this is not a wide enough range of technologies that I have to deal with, so I needed to figure out how I could call my web service from within an Access database using Visual Basic for Applications (VBA).

The web service just takes in a couple of form variables, chugs through the database looking for the matching records, and returns a string with the pertinent data. Here is the VBA code that I put together to accomplish this task:

Sub Method1()
 
    Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
    URL = "https://www.mywebsite.com/WebService.asmx/StatusCall"
    objHTTP.Open "POST", URL, False
 
    objHTTP.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
    objHTTP.send "formVariable1=value1&formVariable2=value2"
 
    If objHTTP.Status = 200 Then
        MsgBox objHTTP.responseText
    Else
        MsgBox "Failed at getting response from web service:" & objHTTP.Status
    End If
 
End Sub

The response text is being sent to a message box, but you could just as easily return that string, or better yet, use an XML parser to dig into the data for just what you need. Perhaps I will save that code for another post later.

BTW, Happy Birthday to Julie Newmar, by far the best Catwoman from any Batman live action or animated TV show or movie.

Multiplicative persistence

I saw a story this morning in Gizmodo about hard logic puzzles, and one of them caught my attention for some brute force computational processing.

Martin Gardner was a famous mathematician, and in one of his books, he talks about the persistence of a number as the number of times it iteratively takes to multiply the individual digits of an integer together until you end up with a single digit number. As an example, if you consider the number 25, the persistence is 2 because 2×5 equals 10 (step 1), and then 1×0 equals 0 (step 2).

So here is a quick algorithm I threw together in VB.NET to find the smallest number that has a particular persistence:

Module Module1
 
    Function Persistence(num As Integer) As Integer
 
        Dim n As Integer = Math.Abs(num)
        Dim p As Integer = 0
        While n >= 10
            Dim nString = n.ToString()
            n = 1
            For i = 0 To nString.Length - 1
                n *= Convert.ToInt16(nString.Substring(i, 1))
            Next
            p = p + 1
        End While
 
        Return p
 
    End Function
 
    Sub Main()
 
        Dim persistenceList As List(Of Integer) = New List(Of Integer)()
 
        Dim p As Integer
        For i = 1 To 10000000
            p = Persistence(i)
            If persistenceList.Count < p Then
                Console.WriteLine("Just found a persistence of " + p.ToString() + " for " + i.ToString())
                persistenceList.Add(i)
            End If
        Next
 
        Console.WriteLine("Press any key to exit...")
        Console.ReadKey()
 
    End Sub
 
End Module

When you run this, you see something like this in the console:

Just found a persistence of 1 for 10
Just found a persistence of 2 for 25
Just found a persistence of 3 for 39
Just found a persistence of 4 for 77
Just found a persistence of 5 for 679
Just found a persistence of 6 for 6788
Just found a persistence of 7 for 68889
Just found a persistence of 8 for 2677889
Press any key to exit...

Above a persistence of 8, it can take a very very long time to brute force a solution.

BTW, Happy Birthday to Anton Fig, a fantastic drummer who has worked with one of my favorite artists, Joe Bonamassa.

Centering the toolbarItems in a UINavigationController

When you add to the toolbarItems array of your UINavigationController, it puts the items left justified in the toolbar at the bottom of the screen. If you instead want the items to be centered in the toolbar, here is a way to do it…

NSArray *itemArray = @ [ @"Item 1", @"Item 2" ];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
segmentedControl.frame = CGRectMake(0.0, 0.0, 240.0, 30.0);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:@selector(segmentedControlChanged:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:segmentedControl] autorelease];
UIBarButtonItem *flex1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL] autorelease];
UIBarButtonItem *flex2 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL] autorelease];
self.toolbarItems = @ [ flex1, item, flex2 ];
[segmentedControl release];

The flex1 and flex2 items are just like adding the springs in Interface Builder, which space things out equally across the view. This code is not ARC approved as you can see from the autorelease and release methods, but if you need to use it in an ARC project you should be able to figure it out.

BTW, it looks to be a thin day for actor or musician birthdays, deaths, events, or holidays, so Happy Birthday to President Obama.