VBA Snippets: Open a File for Appending

MicrosoftThis post is part of the series on VBA Snippets.

The below VBA script can be used to open a text file for appending and then write a line to the bottom of the file; it could be combined with the example in this eries’ last post on selecting records from a Microsoft Dynamics ODBC connection to out put multiple lines.

The highlighted section is the path of the output file.

Dim objFSO As FileSystemObject
Dim objTextStream As TextStream

Const fsoForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objTextStream = objFSO.OpenTextFile(output path, fsoForAppending, True)

'Write line
objTextStream.WriteLine data to output

objTextStream.Close
Set objTextStream = Nothing

VBA Snippets: Select Records from Microsoft Dynamics ODBC Connection

MicrosoftThis post is part of the series on VBA Snippets.

In yesterdays post, I covered adding an ODBC connection to Microsoft Dynamics GP VBA for use n windows or reports. The below is an example of a SQL query using the ODBC connection.

SOPType and SopNUmber (highlighted) are fields from a window added to the VBA.

This example retries a list of fields from the Sales Transaction Amounts Work (SOP10200) table.

Dim objRS As ADODB.RecordSet
Set objRS = New ADODB.RecordSet
Set objRS.ActiveConnection = madoConn
sSQL = "SELECT * FROM SOP10200 WHERE SOPTYPE = " & SOPType & " AND SOPNUMBE = '" & SOPNumber & "'"
objRS.Source = sSQL
objRS.Open

If objRS.State = adStateOpen Then
	If Not (objRS.BOF Or objRS.EOF) Then
		objRS.MoveFirst
		
		Do While Not objRS.EOF
			' your code goes here; reference fields using objRS.fields("fieldname"))
		
			objRS.MoveNext
		Loop
	End If
	objRS.Close
End If
Set objRS = Nothing

VBA Snippets: Adding an SQL ODBC Connection in Microsoft Dynamics GP

MicrosoftThis post is part of the series on VBA Snippets.

There is an ADO connection available to VBA within Microsoft Dynamics GP which you can use, but there are some steps you need to follow to use it.

The first step is to declare the variable which will hold the connection.

Private madoConn AS ADODB.Connection

Then you need to create the connection which this example does using a Connect subroutine:

Private Sub Connect()
	If madoConn.State <> adStateOpen Then 
		Set madoConn = UserInfoGet.CreateADOConnection
		madoConn.DefaultDatabase = UserInfoGet.IntercompanyID
	End If
End Sub

It checks if the connection is already open and, if not, uses the UserInfoGet object which holds the connection detail exposed in Dynamics GP; I am also using the same object to set the default database property.

Once connected you can use the connection to execute SQL queries; I’ll show some examples of this in later posts.

When you’re finished with the connection, you can close and destroy the connection:

Private Sub Disconnect()
    If madoConn.State = adStateOpen Then madoConn.Close
    Set madoConn = Nothing
End Sub

VBA Snippets: Execute URL or Application

MicrosoftThis post is part of the series on VBA Snippets.

it is possible to execute a URL or application in VBA using the Windows Shell Execute API function. In this snippet I am executing a URL, but this could be an application.

Before you can call ShellExecute in code you need to add the following line to the declarations at the top of the mdoule:

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

The following VBA command will execute the supplied URL (or application); the highlighted variable contains the URL:

iResult = ShellExecute(0, "open", sReportURL, vbNullString, vbNullString, vbNormalFocus)

You can then use the iResult for error handling.

VBA Snippets: Sleep

MicrosoftThis post is part of the series on VBA Snippets.

The following VBA snippets can be used to set a pause (sleep) in the code.

The first one needs to be in the declarations at the top of the module:

Private Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)

The second is used where you want the code to pause (my recent use was to pause for five seconds after an error and before retrying the action):

Sleep (1000 * 5)

The highlighted section is the time; 1000 milliseconds multiplied by 5 to give me 5 seconds.

VBA Snippets: Series Index

MicrosoftWe’ve recently taken on a new client for support of Microsoft Dynamics GP who has a number of modified forms and reports which have been extended with VBA code to add additional functionality. I have done a reasonable amount of VBA and VB6 in the past, but that was sometime ago and I’ve found myself searching online for examples on how to do some things when they’ve asked for further modifications, so I’ve decided that I’ll post snippets of VBA code here so I can easily find how to do things.

I’ve already been doing similar posts with Network Shell Snippets and PowerShell Snippets. The series index will automatically update as posts go live, but if you’re reading a syndicated version, you’ll need to check back to the original post

VBA Snippets
Sleep
Execute URL or Application
Adding an SQL ODBC Connection in Microsoft Dynamics GP
Select Records from Microsoft Dynamics ODBC Connection
Open a File for Appending