|
Steps for setting up the WinWedge
Steps for setting up MS ACCESS
Dealing with Multiple Fields
Troubleshooting
1. Select "DDE Server" from the "Mode" menu
in the Wedge. When the dialog box appears asking for a DDE
Command Destination Application, enter: "MSACCESS" as
the Application Name and the name of your open database (without
the MDB Extension) as the DDE topic.
Note: If you compile your Access database to an MDE file, then you will need to change the DDE Topic to the name of the MDE file including the MDE file name extension. For example, if you compile your database and create a MDE file named MyData.MDE, you will need to specify "MyData.MDE" for the DDE Topic. You do not need to include the file path for the MDE file and should only specify the file name with the MDE extension.
The above also applies to MDB files that are used with the Microsoft Access Runtime engine. For example, if you will be using your MDB file with the Access Runtime engine, then you will need to change the DDE Topic to the name of the MDB file including the ".MDB" file name extension.
2. Select "Input Record Structure" in the "Define" menu
and define the structure of the input data to WinWedge. When
you get to the final Window entitled "Input Record Definition
Editor", enter the string: [GrabData] as the
Field Postamble DDE Command after the last data field that
you have defined. This is a DDE command that will be sent
to MSACCESS after each data record is received by the Wedge.
3. Set up the rest of the Wedge as needed for your
serial device and then activate it.
MSAccess does not have a "GrabData" DDE command
so the first step is to create one. When you define a macro
in an Access database, the name of the macro automatically
becomes a valid MSAccess DDE command for as long as the database
is open.
1. Create a new macro that performs the "RunCode" Action.
For the function name that is required by the RunCode action,
enter the name "GetWedgeData()". Save the
macro with the macro name "GrabData" then
switch to the Modules tab and create a new module containing
the code below.
| |
Function GetWedgeData
()
' Declare variables
Dim ChannelNumber,
MyData As Variant
'
open a link to WinWedge
ChannelNumber
= DDEInitiate("WinWedge", "Com1")
'
get data from field(1)
MyData = DDERequest(ChannelNumber, "FIELD(1)")
'
terminate the link
DDETerminate ChannelNumber
'
if no data then quit
If Len(MyData) = 0 Then Exit Function
'Data
from Field(1) in WinWedge is now in the variable "MyData".
The following
' code adds a new record to TABLE1 and stores the data in a field named
' "SerialData"
Dim MyDB,
MyTable
Set MyDB = CurrentDB()
Set MyTable = MyDB.OpenRecordset("TABLE1")
MyTable.AddNew '
add a new record
'
write our data to a field named "SerialData"
MyTable![SerialData] = MyData
MyTable.Update '
update the table
MyTable.Close
'
close the table '
Destroy objects
Set MyDB =
Nothing
Set MyTable = Nothing
End Function
|
When finished, save your Module as modWedgeCode. The example
above sets up the Wedge to issue a DDE command consisting
of the name of an Access macro (directly to the Access
database
that contains the macro) after each data record is received
from your serial device. The macro calls an Access VBA function
that performs a DDERequest back to the Wedge and returns
the contents of FIELD(1) to a variable named "MyData".
It then creates a new record in a table and inserts the data
into the new record.
If your record contains more than one field you will need
to modify the code slightly to enter the additional fields
into your table. Suppose for example that your record looks
like:
X = 0.987, Y = 0.786, Z = 1.23<CR><LF>
You would first configure WinWedge for three, comma delimited
fields with a Carriage Return or CRLF Received as the end
of Record. You would probably also apply a numeric filter
to each field to strip out the "X = ". Then your
code would look like:
| |
Function
GetWedgeData ()
'
Declare variables
Dim ChannelNumber,
MyDataX, MyDataY, MyDataZ
'
open a link to WinWedge
ChannelNumber
= DDEInitiate("WinWedge", "Com1")
'
get data from field(1)
MyDataX = DDERequest(ChannelNumber, "FIELD(1)")
MyDataY = DDERequest(ChannelNumber, "FIELD(2)")
MyDataZ = DDERequest(ChannelNumber, "FIELD(3)")
'
terminate the link
DDETerminate ChannelNumber
Dim MyDB,
MyTable
Set MyDB = CurrentDB()
Set MyTable = MyDB.OpenRecordset("TABLE1")
MyTable.AddNew '
add a new record
'
write our data to each field
MyTable![XField] = MyDataX
MyTable![YField] = MyDataY
MyTable![ZField] = MyDataZ
MyTable.Update '
update the table
MyTable.Close
'
close the table '
Destroy objects
Set MyDB
= Nothing
Set MyTable = Nothing
End Function
|
Troubleshooting
Usually this means that you named your Module "GetWedgeData".
You should never give a module the same name as a procedure,
or Access may be unable to find that procedure. If this is
the case then simply rename your module.
Our example uses a table called "Table1" and a
field called "SerialData". You will need to modify
the code slightly so that it opens YOUR table and writes
to fields that exist in that table. Change the lines below
to point to your table and a valid field in that table.:
| |
Set MyTable = MyDB.OpenRecordset("TABLE1")
MyTable![SerialData] = MyData
|
Make sure that the data type for that field is appropriate:
If the MyData variable (the data from WinWedge) contains "X
= 123" and your field can only contain numbers you will
get this error.
Older versions of the WinWedge Manual contain the line:
| |
Dim MyDB as Database, MyTable As Recordset
|
However MS Access 2000 and later no longer support the Database
type declaration so the easiest solution is simply to delete
the words "as Database" from the statement:
This error usually means that you forgot to change the com
port in your code - check the line:
| |
ChannelNumber = DDEInitiate("WinWedge", "Com1")
|
and change "Com1" to "Com2" or whichever
com port you are using. If you are using TCPWedge the line
should be something like:
| |
ChannelNumber = DDEInitiate("TCPWedge", "123.45.67.89")
|
Be sure to change "123.45.67.89" to the IP Address
TCPWedge is connected to.
The following article explains how to prevent this:
Running Multiple Instances
of WinWedge with Microsoft Access.
This problem has a number of possible causes. First it is
important to realize that the table will not usually update
in real time, in most cases you will need to close the
table and reopen it in order to see the new data. In order
to see the table update in real time you will need to change
the refresh rate. To do so, click on Tools > Options and
change the "Refresh Interval (sec)" value from
its 60 second default to just 1 or 2 seconds.
If you still have no data then make sure that WinWedge is
correctly configured in DDE Server Mode as described at the
top of this page. Next, with WinWedge running and containing
data from your instrument, try running the macro manually
and see if it enters the data into your table.
If the macro works when you run it manually then
there is a problem with the DDE connection between WinWedge
and MS Access. Usually this is caused by long FileNames.
While 32 Bit versions of WinWedge suport long file names,
there seems to be an upper limit to the length of a DDE Topic.
Since Access uses the name of the database as the topic filenames
that exceed this limit may be truncated. Furthermore DDE
Topics that contain spaces may need to be enclosed in quotes.
Suppose your database is called "This is my WinWedge
Test Database.mdb" - then the DDE Topic should be "This
is my WinWedge Test Database" however it may be truncated
to "This is my W" or possibly just "This" if
the topic was not enclosed in quotations. Either way, the
topic does not exist and the macro will not run. Try enclosing
your DDE Topic in quotes or rename your database based on
the old 8.3 short file name rules and then update the Topic
in WinWedge.
If the macro does not work when you run it manually then
there is probably something wrong with the code. Usually
the following line is to blame:
| |
If Len(MyData) = 0 Then Exit Function
|
Before entering data into the table this line checks to
see if there actually is any data in the MyData Variable
and If there is no data it simply exits. If you have made
any customizations to the code you may have changed the variable
names and you are no longer using a variable called MyData.
If this is the case then it will always be empty and the
function will always stop at this line. Delete the line or
modify it to check a variable that does exist and try again.
If the macro still doesn't work you will need to set a break
point near the top of the code and step through it to see
what happens. To set a break point simply place the cursor
on the line:
| |
ChannelNumber = DDEInitiate("WinWedge", "Com1")
|
and Press F9. (It should turn a brown color). Then when
you run the macro again the line will turn Yellow. Keep pressing
F8 to step through the code line by line, place your cursor
over variables to see what is in them and make a note of
which line you are on when it stops or causes an error. When
you are done, be sure to remove the break point using the
same steps you used to add it (F9 will toggle it on and off)
or the code will always stop executing at that point.
Related Links
Launching And Terminating WinWedge
From Access
Collecting Data Directly into
a Form in an Access Database
Transmitting Variables Out
The Serial Port From Access |