I had found an example of some code at bVisual, and when I try to run it, I get an error, stating subscript Range out of Scope.
Public Sub FindRow()
'David Parker : 11/11/2007
'To find a row with a given string in a DataRecordset
Dim findString As String
Dim colString As String
Dim drs As DataRecordset
Dim col As DataColumn
Dim win As Visio.Window
Dim rowids() As Long
Dim i As Integer
Dim msg As String
Dim rowid As Long
Dim vData As Variant
Dim primKeys() As String
Dim primKey As Integer
Dim rowString As String
'Check the active windows to find the window
For Each win In Visio.ActiveWindow.Windows
'The External Data window has ID = 2044
If win.ID = 2044 Then
Set drs = win.SelectedDataRecordset
Exit For
End If
Next win
If drs Is Nothing Then
MsgBox "You do not have a recordset selected!"
Exit Sub
End If
'Get the primary key (single assumed)
drs.GetPrimaryKey visKeySingle, primKeys
msg = "Enter the number of the column to search:"
i = 1
For Each col In drs.DataColumns
msg = msg & vbCrLf & CStr(i) & vbTab & col.Name
If primKeys(0) = col.Name Then <------This is where the debugger stops
primKey = i
End If
i = i + 1
Next col
colString = InputBox(msg, , "1")
If Not IsNumeric(colString) Then
MsgBox "You must enter a number between 1 and " & drs.DataColumns.Count
Exit Sub
ElseIf CInt(colString) < 1 Or CInt(colString) > (drs.DataColumns.Count) Then
MsgBox "You must enter a number between 0 and " & drs.DataColumns.Count
Exit Sub
End If
findString = InputBox("Enter text to find")
If Len(findString) = 0 Then
Exit Sub
End If
rowids = drs.GetDataRowIDs(drs.DataColumns.Item(CInt(colString)) & " LIKE '" & findString & "'")
If UBound(rowids) > 0 Then
'Need to ask which row
msg = "Select a row to highlight"
msg = msg & vbCrLf & "Row" & vbTab & drs.DataColumns(primKey).Name & _
vbTab & drs.DataColumns(CInt(colString)).Name
For i = 0 To UBound(rowids)
rowid = rowids(i)
vData = drs.GetRowData(rowid)
msg = msg & vbCrLf & CStr(i + 1) & vbTab & vData(primKey - 1) & _
vbTab & vData(CInt(colString) - 1)
Next i
rowString = InputBox(msg, , "1")
Else
'Only one row to select
rowid = rowids(0)
vData = drs.GetRowData(rowid)
rowString = "1"
End If
If Not IsNumeric(rowString) Then
MsgBox "You must enter a number between 1 and " & UBound(rowids) + 1
Exit Sub
ElseIf CInt(rowString) < 1 Or CInt(rowString) > (UBound(rowids) + 1) Then
MsgBox "You must enter a number between 1 and " & UBound(rowids) + 1
Exit Sub
End If
'Select the row in the External Data window
win.SelectedDataRowID = rowids(CInt(rowString) - 1)
End Sub
Hi draco,
We'd need more information. You've copied code from bVisual, which in all likelihood works just fine, then said that it doesn't work on your machine--that just doesn't tell us very much.
Do you have a set of data loaded into your Visio drawing?
If you go to Tools > References in the VBA editor, is anything missing?
On which line does the code break?
Can you compile the VBA code? (Debug > Compile project_name)
Cheers,
Chris
Hi Chris
I do have a set of data loaded in the external data window.
It does not appear as if Im missing anything I would need to run this code sample. Would there be anything in particular I should make sure I have loaded?
I marked in my tagged code the line where the debugger gives the error, it is in the for loop that retrieves the primary key.
It will compile, but, has an error at runtime.