• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

bVisual

  • Home
  • Services
    • How Visio smartness can help your business
    • Visio visual in Power BI
    • Visio Consulting Services
    • Visio Bureau Services
    • Visio Training and Support Services
  • Products
    • Visio Shape Report Converter
    • SS Plus
    • LayerManager
    • visViewer
    • Metro Icons
    • Rules Tools for Visio
    • The Visio 2010 Sessions App
    • Multi-Language Text for Visio
    • Document Imager for Visio
    • multiSelect for Visio
    • pdSelect for Visio
  • Case Studies
    • Case studies overview
    • Using Visio in Education for GIS
    • Visualizing Construction Project Schedules
    • Visio Online Business Process Mapping
    • Nexans Visio Template
    • CNEE Projects, WorldCom
    • Chase Manhattan Bank
  • News
    • Recent news
    • News archive
  • Resources
    • Articles➡
      • ShapeSheet Functions A-Z
      • Comparing Visio for the Web and Desktop
      • Customising Visio Shapes for the Web App
      • Key differences between the Visio desktop and web apps
      • Using the Visio Data Visualizer in Excel
      • Using Visio in Teams
      • Creating Visio Tabs and Apps for Teams with SharePoint Framework (SPFx)
      • Designing Power Automate Flows with Microsoft Visio
      • Innovative uses of Visio Lists
    • Webcasts ➡
      • Visio in Organizations
      • My session and other Visio sessions at MSIgnite 2019
      • Power up your Visio diagrams
      • Vision up your Visio diagrams
      • The Visio 2010 MVP Sessions
    • Visio Web Learning Resources
    • Books➡
      • Visualize Complex Processes with Microsoft Visio
      • Mastering Data Visualization with Microsoft Visio
      • Microsoft Visio Business Process Diagramming and Validation
      • Visualizing Information with Microsoft Visio
  • Blog
    • Browse blog articles
    • Visio Power BI articles
    • Visio for Web articles
    • A history of messaging and encryption
  • About us
    • About bVisual
    • Testimonials
    • Bio of David Parker
    • Contact Us
    • Website Privacy Policy
    • Website terms and conditions
    • Ariba Network
You are here: Home / Visio / Finding a Row in a Visio DataRecordset

Published on November 11, 2007 by David Parker

Finding a Row in a Visio DataRecordset

A recent newsgroup post asked how to find a row in a Visio DataRecordset for a given criteria.  Well, I have used the IT Asset Management sample in Visio 2007 Professional to demonstrate how this can be done with a VBA function.

Of course, you may wish to improve the user interface because I limited myself to using an InputBox, and I have assumed that there is only one column defining the primary key of the DataRecordset.

The sub function, FindRow, listed below first checks whether the External Data window is open, and then gets the active DataRecordset.  The code then reads the columns of the DataRecordset and prompts for a selection of the number of the column that you wish to search for a value in.

image

The code will abort if an incorrect number is entered, otherwise you will be asked to enter the text to find in the column.  Note that you can enter a wildcard “*” character.

image

You will then be prompted to enter the row number of the row that you want to navigate to in the External Data window.  Note that you will not be prompted to select a row if there is only one matching row.

image

When you enter a row number, the code gets the related row ID, and uses this to select the row in the External Data window.

image

Here is a listing of the VBA code to find a row by criteria:

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
            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

Related

Filed Under: External Data, VBA, Visio

About David Parker

David Parker has 25 years' experience of providing data visualization solutions to companies around the globe. He is a Microsoft MVP and Visio expert.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

  • LinkedIn
  • Twitter

Recent Posts

  • Fixing dimensions of 2D shapes
  • Merging Linked Data from Similar Tables
  • Smart Radio Buttons and Check Boxes in Visio
  • Using Button Face Ids in Visio
  • Grid Snapping Revisited

Categories

Tags

Accessibility Add-Ins Connectors Containers Data Export Data Graphics Data Import Data Visualizer Educational Excel GraphDatabase Hyperlinks Icon Sets JavaScript LayerManager Layers Legend Link Data to Shapes Lists MSIgnite MVP Office365 Org Chart PowerApps PowerBI PowerQuery Processes Setup and Deployment Shape Data Shape Design ShapeSheet ShapeSheet Functions SharePoint 2013 SQL Teams Validation VBA Video Visio Visio 2007 Visio for the Web Visio Online Visio Services Visio Viewer Webinar

Footer

bVisual Profile

The UK-based independent Visio consultancy with a worldwide reach. We have over 25 years experience of providing data visualization solutions to companies around the globe.

Learn more about bVisual

  • Amazon
  • E-mail
  • Facebook
  • LinkedIn
  • Twitter
  • YouTube

Search this website

Recent posts

  • Fixing dimensions of 2D shapes
  • Merging Linked Data from Similar Tables
  • Smart Radio Buttons and Check Boxes in Visio
  • Using Button Face Ids in Visio
  • Grid Snapping Revisited

Copyright © 2025 · Executive Pro on Genesis Framework · WordPress · Log in