Automatic record linking and insert.

J

jeff

Hello - I have Visio 2003 SP1 and Access 2003. I have created a drawing (7
shapes), exported to an access table and then subsequently linked the drawing
to the table. However, when I try to refresh a database record with new
shape text Visio forces me to manually link the shape to a record in the
table. After that I can update the shape properties in either Access or
Visio.
Is there a way to automatically link the shape to its record? maintian the
link?

Also, when I drag a new shape on to the drawing I cannot figure out how to
create a new record in the database without exporting the drawing and
replacing the existing table. What is the method for inserting a new record
in the table when I drop the shape on the drawing?

Thanks
 
T

twoods

have you received a response yet? I am trying to the exact same thing. I
want to do visual designs using our parts library, then when satisifed
with design, run a macro funtion and insert are new record(s) into the
database that wil be a basis for a customer quote.

please send me any solution to (e-mail address removed). IF you send me your
email I wil let you kow what I find as well

thanks and good luck
twoods
 
A

Al Edlund

my experience with the tool is that it was a batch process for creation so
you would probably have to write some custom code to add additional shapes
to the drawing. I suspect that there will be more integration in the future
based on some of what I see in both vs.net 2005 beta and sql server 2005
beta for data diagramming and some of the new wizards.
al
 
J

jeff

Thanks for responding. Unfortunately I am not a programmer but am willing to
try. Can you or anyone point me toward some VBA code snippet/example that
would iterate through shapes in a linked drawing and perform the "select
database record" command on each shape's respective records? Also, where can
I find some visio VBA commands for database activities like linking to the
database, selecting records, inserting records (based on the selected shape
properties)? I have combed through quite a bit if the documentation but have
not been able to find anything. Cheers.
 
A

Al Edlund

being aware of the fact that VBA is a subset of VB(6), I usually point
people over at the MSDN downloads. They have a great package of 101 examples
that are generally easily modified for VBA. The unfortunate part is that
database is not on the low end of the scale for challenges but does bring a
great sense of reward when you get them to work. Here's some examples for
VBA DB for create, update, and select.

al


' this subroutine creates a database record using a passed string
' GUID (usually from the pagObj_ShapeAdded event defined in ThisDocument)

Public Sub subCreateDbRecord(strDbTable As String, strIndex As String,
strGUID As String)

Dim intResult As Integer
Dim str_db_filename As String
Dim db As Database
Dim visDocument As Visio.Document
Dim visPage As Visio.Page
Dim SaveErr As Long
Dim errDB As ADODB.Error


Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset

Dim strProvider As String
Dim strSource As String
Dim strConn As String
Dim strSelect As String
strProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
strSource = "Data Source="
Dim strProviderED As String ' the end delimter
strProviderED = ";"

On Error GoTo CreateRecord_Err

' first we have to find out which database is associated with this
document
' it is stored in a custom property on the sheet named "project
definition"
Set visDocument = Visio.ActiveDocument
Set visPage = visDocument.Pages.item("Project Definition")
str_db_filename = visDocument.Path &
visPage.PageSheet.Cells("prop.database_file.value").ResultStr("")

' now create the connection string and open the database
strConn = strProvider & strSource & str_db_filename & strProviderED
cnn.Open strConn

' all shape properties will be saved in a table base on group
strSelect = "SELECT * FROM " & strDbTable
rst.Open strSelect, cnn, adOpenKeyset, adLockOptimistic

rst.AddNew
rst.Fields(strIndex) = strGUID

rst.Update

rst.Close
cnn.Close
DoEvents

CreateRecord_Exit:

Exit Sub

CreateRecord_Err:
SaveErr = Err.Number

If SaveErr > 0 Then
Debug.Print "Err in subCreateDbRecord is " & Err & " " & Err.Description
Resume Next
End If

For Each errDB In cnn.Errors
Debug.Print "DB Create"
Debug.Print "DB Description: " & errDB.Description
Debug.Print "DB Number: " & errDB.Number & " (" & _
Hex$(errDB.Number) & ")"
Debug.Print "JetErr: " & errDB.SQLState
Next

End Sub



' this is used by the cell changed event to allow us to just change a
' single cell in a record, rather than full record updates. work in
' progress

Public Sub subDiscreteFieldUpdate(strTable As String, strIndex As String,
strGUID As String, strField As String, strValue As String)
Dim intResult As Integer
Dim str_db_filename As String
Dim db As Database
Dim visDocument As Visio.Document
Dim visPage As Visio.Page
Dim SaveErr As Long
Dim errDB As ADODB.Error

Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset

Dim strProvider As String
Dim strSource As String
Dim strConn As String
Dim strSelect As String

strProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
strSource = "Data Source="
Dim strProviderED As String
strProviderED = ";"

On Error GoTo DiscreteField_Exit

'first we have to find out which database is associated with this
document
Set visDocument = Visio.ActiveDocument
Set visPage = visDocument.Pages.item("Project Definition")
str_db_filename = visDocument.Path &
visPage.PageSheet.Cells("prop.database_file.value").ResultStr("")

strConn = strProvider & strSource & str_db_filename & strProviderED
cnn.Open strConn

strSelect = "SELECT * FROM " & strTable & " Where " & strIndex & " =
" & strGUID
rst.Open strSelect, cnn, adOpenKeyset, adLockOptimistic

If (rst.BOF And rst.EOF) Then
' Debug.Print "err discrete field update " & strGUID & " record
not found"
GoTo DiscreteField_Exit
End If

' MsgBox strValue
If strField = "propcost" Then
' Debug.Print "propcost " & Int(strValue)
rst.Fields(strField).Value = Int(strValue)
Else
rst.Fields(strField).Value = strValue
End If
rst.Update


DiscreteField_Exit:
rst.Close
cnn.Close
DoEvents

Exit Sub

DiscreteField_Err:
SaveErr = Err.Number
If SaveErr <> 0 Then
Debug.Print "Err in subDiscreteFieldUpdate is " & Err & " " &
Err.Description
Debug.Print strGUID & " " & strField & " " & strValue

For Each errDB In cnn.Errors
Debug.Print "DB Update " & " " & strGUID & " " & strField & " "
& strValue
Debug.Print "DB Description: " & errDB.Description
Debug.Print "DB Number: " & errDB.Number & " (" & _
Hex$(errDB.Number) & ")"
Debug.Print "JetErr: " & errDB.SQLState
Next
' Resume DiscreteField_Exit:
End If

End Sub


' this is where we take the masterid and do a search
' in the shape table to see if we have a match. The
' table setup is pretty straight forward and works
' as a fair prototype. Originally this was where the
' vent add-on kept track of the different stencils. The
' only change was the addition of a cost field.

Public Function funcTestDbForVent(strGUID As String) As Boolean

Dim visDocument As Visio.Document
Dim visPage As Visio.Page
Dim SaveErr As Long
Dim errDB As ADODB.Error
Dim errNum As Integer

Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim errLoop As ADODB.Error
Dim strError As String

Dim strProvider As String
Dim strSource As String
Dim strConn As String
Dim strfind As String
Dim strSelect As String
Dim strValue As String
Dim strField As String
Dim str_db_filename As String

strProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
strSource = "Data Source="
Dim strProviderED As String ' the end delimiter
strProviderED = ";"

On Error GoTo TestDbForVent_Err

'first we have to find out which database is associated with this
document
Set visDocument = Visio.ActiveDocument
Set visPage = visDocument.Pages.item("Project Definition")
str_db_filename = visDocument.Path &
visPage.PageSheet.Cells("prop.database_file.value").ResultStr("")

' now we can connect to the database
strConn = strProvider & strSource & str_db_filename & strProviderED
cnn.Open strConn
strfind = "ShapeID=" & strGUID & strProviderED
strSelect = "SELECT * FROM tblShapeMaster WHERE " & strfind
rst.Open strSelect, cnn, adOpenKeyset, adLockOptimistic

If (rst.BOF And rst.EOF) Then ' we dont have it in the database
rst.Close
cnn.Close
' Debug.Print "GUID = " & strGUID & " record not found during
vent validate"
funcTestDbForVent = False
Exit Function
End If

' add code here if you want to do something with what came back


' Debug.Print "GUID = " & strGUID & "Found in VENT "
funcTestDbForVent = True

rst.Close
cnn.Close

TestDbForVent_Exit:

DoEvents

Exit Function

TestDbForVent_Err:

' Enumerate Errors collection and display
' properties of each Error object.
For Each errLoop In cnn.Errors
strError = "ADODB Error #" & errLoop.Number & vbCr & _
" " & errLoop.Description & vbCr & _
" (Source: " & errLoop.Source & ")" & vbCr & _
" (SQL State: " & errLoop.SQLState & ")" & vbCr & _
" (NativeError: " & errLoop.NativeError & ")" & vbCr
If errLoop.HelpFile = "" Then
strError = strError & _
" No Help file available" & _
vbCr & vbCr
Else
strError = strError & _
" (HelpFile: " & errLoop.HelpFile & ")" & vbCr & _
" (HelpContext: " & errLoop.HelpContext & ")" & _
vbCr & vbCr
End If
Debug.Print strError
Next errLoop

SaveErr = Err
' object not bound in get_part_id means no unique id could be valid with
no cost
If SaveErr = 91 Then
SaveErr = 0
Err.Clear
Resume Next
End If
If SaveErr <> 0 Then
Debug.Print "Err in TestDbForVent is " & Err & " " & Err.Description
Err.Clear
End If

Resume TestDbForVent_Exit

End Function
 
J

jeff

Thanks for advice - truly helpful.

Al Edlund said:
being aware of the fact that VBA is a subset of VB(6), I usually point
people over at the MSDN downloads. They have a great package of 101 examples
that are generally easily modified for VBA. The unfortunate part is that
database is not on the low end of the scale for challenges but does bring a
great sense of reward when you get them to work. Here's some examples for
VBA DB for create, update, and select.

al


' this subroutine creates a database record using a passed string
' GUID (usually from the pagObj_ShapeAdded event defined in ThisDocument)

Public Sub subCreateDbRecord(strDbTable As String, strIndex As String,
strGUID As String)

Dim intResult As Integer
Dim str_db_filename As String
Dim db As Database
Dim visDocument As Visio.Document
Dim visPage As Visio.Page
Dim SaveErr As Long
Dim errDB As ADODB.Error


Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset

Dim strProvider As String
Dim strSource As String
Dim strConn As String
Dim strSelect As String
strProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
strSource = "Data Source="
Dim strProviderED As String ' the end delimter
strProviderED = ";"

On Error GoTo CreateRecord_Err

' first we have to find out which database is associated with this
document
' it is stored in a custom property on the sheet named "project
definition"
Set visDocument = Visio.ActiveDocument
Set visPage = visDocument.Pages.item("Project Definition")
str_db_filename = visDocument.Path &
visPage.PageSheet.Cells("prop.database_file.value").ResultStr("")

' now create the connection string and open the database
strConn = strProvider & strSource & str_db_filename & strProviderED
cnn.Open strConn

' all shape properties will be saved in a table base on group
strSelect = "SELECT * FROM " & strDbTable
rst.Open strSelect, cnn, adOpenKeyset, adLockOptimistic

rst.AddNew
rst.Fields(strIndex) = strGUID

rst.Update

rst.Close
cnn.Close
DoEvents

CreateRecord_Exit:

Exit Sub

CreateRecord_Err:
SaveErr = Err.Number

If SaveErr > 0 Then
Debug.Print "Err in subCreateDbRecord is " & Err & " " & Err.Description
Resume Next
End If

For Each errDB In cnn.Errors
Debug.Print "DB Create"
Debug.Print "DB Description: " & errDB.Description
Debug.Print "DB Number: " & errDB.Number & " (" & _
Hex$(errDB.Number) & ")"
Debug.Print "JetErr: " & errDB.SQLState
Next

End Sub



' this is used by the cell changed event to allow us to just change a
' single cell in a record, rather than full record updates. work in
' progress

Public Sub subDiscreteFieldUpdate(strTable As String, strIndex As String,
strGUID As String, strField As String, strValue As String)
Dim intResult As Integer
Dim str_db_filename As String
Dim db As Database
Dim visDocument As Visio.Document
Dim visPage As Visio.Page
Dim SaveErr As Long
Dim errDB As ADODB.Error

Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset

Dim strProvider As String
Dim strSource As String
Dim strConn As String
Dim strSelect As String

strProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
strSource = "Data Source="
Dim strProviderED As String
strProviderED = ";"

On Error GoTo DiscreteField_Exit

'first we have to find out which database is associated with this
document
Set visDocument = Visio.ActiveDocument
Set visPage = visDocument.Pages.item("Project Definition")
str_db_filename = visDocument.Path &
visPage.PageSheet.Cells("prop.database_file.value").ResultStr("")

strConn = strProvider & strSource & str_db_filename & strProviderED
cnn.Open strConn

strSelect = "SELECT * FROM " & strTable & " Where " & strIndex & " =
" & strGUID
rst.Open strSelect, cnn, adOpenKeyset, adLockOptimistic

If (rst.BOF And rst.EOF) Then
' Debug.Print "err discrete field update " & strGUID & " record
not found"
GoTo DiscreteField_Exit
End If

' MsgBox strValue
If strField = "propcost" Then
' Debug.Print "propcost " & Int(strValue)
rst.Fields(strField).Value = Int(strValue)
Else
rst.Fields(strField).Value = strValue
End If
rst.Update


DiscreteField_Exit:
rst.Close
cnn.Close
DoEvents

Exit Sub

DiscreteField_Err:
SaveErr = Err.Number
If SaveErr <> 0 Then
Debug.Print "Err in subDiscreteFieldUpdate is " & Err & " " &
Err.Description
Debug.Print strGUID & " " & strField & " " & strValue

For Each errDB In cnn.Errors
Debug.Print "DB Update " & " " & strGUID & " " & strField & " "
& strValue
Debug.Print "DB Description: " & errDB.Description
Debug.Print "DB Number: " & errDB.Number & " (" & _
Hex$(errDB.Number) & ")"
Debug.Print "JetErr: " & errDB.SQLState
Next
' Resume DiscreteField_Exit:
End If

End Sub


' this is where we take the masterid and do a search
' in the shape table to see if we have a match. The
' table setup is pretty straight forward and works
' as a fair prototype. Originally this was where the
' vent add-on kept track of the different stencils. The
' only change was the addition of a cost field.

Public Function funcTestDbForVent(strGUID As String) As Boolean

Dim visDocument As Visio.Document
Dim visPage As Visio.Page
Dim SaveErr As Long
Dim errDB As ADODB.Error
Dim errNum As Integer

Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim errLoop As ADODB.Error
Dim strError As String

Dim strProvider As String
Dim strSource As String
Dim strConn As String
Dim strfind As String
Dim strSelect As String
Dim strValue As String
Dim strField As String
Dim str_db_filename As String

strProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
strSource = "Data Source="
Dim strProviderED As String ' the end delimiter
strProviderED = ";"

On Error GoTo TestDbForVent_Err

'first we have to find out which database is associated with this
document
Set visDocument = Visio.ActiveDocument
Set visPage = visDocument.Pages.item("Project Definition")
str_db_filename = visDocument.Path &
visPage.PageSheet.Cells("prop.database_file.value").ResultStr("")

' now we can connect to the database
strConn = strProvider & strSource & str_db_filename & strProviderED
cnn.Open strConn
strfind = "ShapeID=" & strGUID & strProviderED
strSelect = "SELECT * FROM tblShapeMaster WHERE " & strfind
rst.Open strSelect, cnn, adOpenKeyset, adLockOptimistic

If (rst.BOF And rst.EOF) Then ' we dont have it in the database
rst.Close
cnn.Close
' Debug.Print "GUID = " & strGUID & " record not found during
vent validate"
funcTestDbForVent = False
Exit Function
End If

' add code here if you want to do something with what came back


' Debug.Print "GUID = " & strGUID & "Found in VENT "
funcTestDbForVent = True

rst.Close
cnn.Close

TestDbForVent_Exit:

DoEvents

Exit Function

TestDbForVent_Err:

' Enumerate Errors collection and display
' properties of each Error object.
For Each errLoop In cnn.Errors
strError = "ADODB Error #" & errLoop.Number & vbCr & _
" " & errLoop.Description & vbCr & _
" (Source: " & errLoop.Source & ")" & vbCr & _
" (SQL State: " & errLoop.SQLState & ")" & vbCr & _
" (NativeError: " & errLoop.NativeError & ")" & vbCr
If errLoop.HelpFile = "" Then
strError = strError & _
" No Help file available" & _
vbCr & vbCr
Else
strError = strError & _
" (HelpFile: " & errLoop.HelpFile & ")" & vbCr & _
" (HelpContext: " & errLoop.HelpContext & ")" & _
vbCr & vbCr
End If
Debug.Print strError
Next errLoop

SaveErr = Err
' object not bound in get_part_id means no unique id could be valid with
no cost
If SaveErr = 91 Then
SaveErr = 0
Err.Clear
Resume Next
End If
If SaveErr <> 0 Then
Debug.Print "Err in TestDbForVent is " & Err & " " & Err.Description
Err.Clear
End If

Resume TestDbForVent_Exit

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top