Sidebar

Visual Basic Database Access Made Easy

Visual Basic
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

If you've been eager to start developing Visual Basic (VB) applications to access your AS/400 data, this article is for you! The VB interactive tutorial (activated by selecting Learning Microsoft Visual Basic from the VB Help menu) is an excellent place to learn about forms, controls, properties, and event-driven programming, but it doesn't teach you a thing about accessing your AS/400 data. I'll assume you either have taken the tutorial or have some basic knowledge of how to write a standalone VB application. I'll show you how to build on that knowledge by teaching you some simple VB techniques that will help you make the transition into the world of client/server programming.

Getting your first AS/400 client/server application up and running teaches you many important concepts. To begin the excursion to full client/server enlightenment, I'll show you how to create a very simple client/server program that accesses your AS/400 database. I'll explain, step-by-step, how to create a sample program. The program will display the records from the QCUSTCDT file (or table) in the QIWS library, but you can modify the program to read any AS/400 database file.

The database access concepts you learn here will serve as a foundation upon which you can build. You need to learn many things to create a professional client/server application, but these basics will give you a place to start.

Before I show you how to create the sample application, let me briefly explain what it does. When the program runs, it asks you to select an ODBC data source, as shown in 1. (ODBC is a method of accessing external data from some PC applications, including VB. If you don't have an ODBC data source configured, check out some of the ODBC articles listed in the related reading box on page 107 for information on how to configure one.)

Before I show you how to create the sample application, let me briefly explain what it does. When the program runs, it asks you to select an ODBC data source, as shown in Figure 1. (ODBC is a method of accessing external data from some PC applications, including VB. If you don't have an ODBC data source configured, check out some of the ODBC articles listed in the related reading box on page 107 for information on how to configure one.)

In the dialog box shown in 1, you choose your AS/400 ODBC data source and press the OK button. This establishes a connection to the AS/400 and retrieves your data. The sample application is displayed as shown in 2.

In the dialog box shown in Figure 1, you choose your AS/400 ODBC data source and press the OK button. This establishes a connection to the AS/400 and retrieves your data. The sample application is displayed as shown in Figure 2.

To navigate through the records in the table, you click on the arrow buttons of the data control. The inner arrows move you backward and forward through the table one record at a time. The outer arrows with the vertical bars move you to the beginning or end of the table. As you move through the records in the table, you see the information change to reflect the field values from the current record.

The tool that allows you to create the link to the AS/400 is a data control. The data control allows you to access your AS/400 database and navigate through the records in the table. It also manages the connection with the AS/400 database and retrieves the records from the database. VB retrieves records into a special object called a recordset, which you can refer to programmatically when you require more advanced functionality. You can create recordsets from ODBC data sources using SQL statements.

As you have seen, data controls also provide the ability to navigate through the records in the recordset. Whenever you move to a different record using the data control, it automatically retrieves the field values for the new record. Bound controls then display the field values for that record on the form.

Bound controls are VB controls that you can use to display data retrieved by a data control. They are called "bound" because one way to use them is to "bind" them to a particular field in the data control's recordset. Every time the data control retrieves a record passed by the database, it sends the values for each field in that record to the corresponding controls that are bound to it. Bound controls require no code to display the values from a data control, so they are a tremendous time-saver when developing your first client/server application.

You can use bound controls to display data from the database in its appropriate format. This example uses standard VB label controls bound to a data control to display the data from the fields. Many other types of controls can be bound to a data control to display recordset data in almost any way you can imagine.

Now that you've seen what this application does, I'll show you how to create it. The application (shown in VB design mode in 3) originates from a blank VB form. First, you place a data control on the form (Label A in 3). Then, you set properties for the data control to configure it to access the AS/400 database (see 4). The Connect property is set to ODBC; (Label A in 4). Setting it to this value instructs VB to display a list of available ODBC data sources so you can choose which one to use when the program runs. I also set the value of the data control's ReadOnly property to True (Label B in 4) to avoid inadvertently updating the data.

Now that you've seen what this application does, I'll show you how to create it. The application (shown in VB design mode in Figure 3) originates from a blank VB form. First, you place a data control on the form (Label A in Figure 3). Then, you set properties for the data control to configure it to access the AS/400 database (see Figure 4). The Connect property is set to ODBC; (Label A in Figure 4). Setting it to this value instructs VB to display a list of available ODBC data sources so you can choose which one to use when the program runs. I also set the value of the data control's ReadOnly property to True (Label B in Figure 4) to avoid inadvertently updating the data.

The SQL statement that returns the records you want is placed in the RecordSource property of the data control (Label C in 4). In this case, the property is set to the value Select * from qiws.qcustcdt. You can set this property to any valid record retrieval SQL statement, so retrieving data from another table is a simple matter of changing this statement. Even complex multiple-table joins can be retrieved with the data control, using more advanced SQL statements.

The SQL statement that returns the records you want is placed in the RecordSource property of the data control (Label C in Figure 4). In this case, the property is set to the value Select * from qiws.qcustcdt. You can set this property to any valid record retrieval SQL statement, so retrieving data from another table is a simple matter of changing this statement. Even complex multiple-table joins can be retrieved with the data control, using more advanced SQL statements.

Because a library is specified in the SQL statement, I had to set the Options property (Label D in 4) of the data control as well. The Options property is actually many properties wrapped up in one. To activate the various functions of this property, you have to turn on the appropriate bit in the property's value. This isn't as bad as it may sound. In this case, all you have to do is supply the value that turns on the SQL passthrough bit. That value is 64. SQL passthrough instructs the VB database engine to pass the SQL string through to the AS/400 instead of trying to process it itself. This is necessary for this example because the VB database engine does not directly support AS/400 libraries from within SQL statements, but the AS/400 does support them.

Because a library is specified in the SQL statement, I had to set the Options property (Label D in Figure 4) of the data control as well. The Options property is actually many properties wrapped up in one. To activate the various functions of this property, you have to turn on the appropriate bit in the property's value. This isn't as bad as it may sound. In this case, all you have to do is supply the value that turns on the SQL passthrough bit. That value is 64. SQL passthrough instructs the VB database engine to pass the SQL string through to the AS/400 instead of trying to process it itself. This is necessary for this example because the VB database engine does not directly support AS/400 libraries from within SQL statements, but the AS/400 does support them.

The next step is to place the bound controls on the form. I used label controls (Label B in 3) because they are useful for displaying read-only data. To bind a label control to a data control, you set two properties. The first property to set is the DataSource property. You set this to the name of the data control to which you wish to bind. Dropping down the list box of this property displays all the data controls on the form (see 5), allowing you to choose one without typing.

The next step is to place the bound controls on the form. I used label controls (Label B in Figure 3) because they are useful for displaying read-only data. To bind a label control to a data control, you set two properties. The first property to set is the DataSource property. You set this to the name of the data control to which you wish to bind. Dropping down the list box of this property displays all the data controls on the form (see Figure 5), allowing you to choose one without typing.

The other label property you must set is the DataField property. You set this property to the name of the field you want to display in the label. Again, dropping down the list box of this property displays all the fields available from the data control if you have set your data control properties correctly (see 6). You must set the data control properties as discussed previously, because the first time you display this list, VB actually logs on to your AS/400 using your data control parameters to retrieve the list of field values.

The other label property you must set is the DataField property. You set this property to the name of the field you want to display in the label. Again, dropping down the list box of this property displays all the fields available from the data control if you have set your data control properties correctly (see Figure 6). You must set the data control properties as discussed previously, because the first time you display this list, VB actually logs on to your AS/400 using your data control parameters to retrieve the list of field values.

Once the data control and the bound fields are on the form, give the fields labels and position them so they look the way you want on the form. Label controls (Label C in 3) are also used to create the field labels. To distinguish the field labels from the actual data fields on the screen, I gave the data fields a border by setting their BorderStyle to 1 - Fixed Single. Then I set the Caption property of the field label controls to the text values that described the field. Once you set these properties, you can run the application and watch it in action.

Once the data control and the bound fields are on the form, give the fields labels and position them so they look the way you want on the form. Label controls (Label C in Figure 3) are also used to create the field labels. To distinguish the field labels from the actual data fields on the screen, I gave the data fields a border by setting their BorderStyle to 1 - Fixed Single. Then I set the Caption property of the field label controls to the text values that described the field. Once you set these properties, you can run the application and watch it in action.

As you can see, creating your first client/server application using a bound data control is relatively simple. No code is required, and you can point and click to lay out the form. The advantage of using the data control is its ease of use, but you pay a price in that it doesn't perform as well as other methods of accessing data from a remote server. For this reason, you'll need to learn many other techniques before you can implement your first successful client/server program into a production environment. However, by taking advantage of the simplicity of the bound data control, you can begin to explore client/server programming to see if it fits in your plans.

Brian Singleton is an associate technical editor for Midrange Computing. He can be reached by E-mail at This email address is being protected from spambots. You need JavaScript enabled to view it..

Related Reading

"Creating Client/Server Applications with ODBC" August 1994

"The ODBC Pre-Flight" January 1995

"ODBC Overview" February 1995

"An Introduction to Event-driven Programming" June 1995

"ODBC Performance Basics" August 1995

Visual Basic Database Access Made Easy

Figure 1: ODBC Data Source Selection


Visual Basic Database Access Made Easy

Figure 2: The Working Example Program


Visual Basic Database Access Made Easy

Figure 3: The Visual Basic Form


Visual Basic Database Access Made Easy

Figure 4: The Data Control Properties Window


Visual Basic Database Access Made Easy

Figure 5: Selecting the Data Control


Visual Basic Database Access Made Easy

Figure 6: Selecting Fields from the Data Control


Brian Singleton
Brian Singleton is former editor of Midrange Computing. He has worked in the IBM midrange arena for many years, performing every job from backup operator to programmer to systems analyst to technology analyst for major corporations and IBM Business Partners. He also has an extensive background in the PC world. Brian also developed a line of bestselling Midrange Computing training videos, authored the bestselling i5/OS and Microsoft Office Integration Handbook, and has spoken at many popular seminars and conferences.

MC Press books written by Brian Singleton available now on the MC Press Bookstore.

i5/OS and Microsoft Office Integration Handbook i5/OS and Microsoft Office Integration Handbook
Harness the power of Microsoft Office while exploiting the iSeries database.
List Price $79.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

RESOURCE CENTER

  • WHITE PAPERS

  • WEBCAST

  • TRIAL SOFTWARE

  • White Paper: Node.js for Enterprise IBM i Modernization

    SB Profound WP 5539

    If your business is thinking about modernizing your legacy IBM i (also known as AS/400 or iSeries) applications, you will want to read this white paper first!

    Download this paper and learn how Node.js can ensure that you:
    - Modernize on-time and budget - no more lengthy, costly, disruptive app rewrites!
    - Retain your IBM i systems of record
    - Find and hire new development talent
    - Integrate new Node.js applications with your existing RPG, Java, .Net, and PHP apps
    - Extend your IBM i capabilties to include Watson API, Cloud, and Internet of Things


    Read Node.js for Enterprise IBM i Modernization Now!

     

  • Profound Logic Solution Guide

    SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation.
    Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects.
    The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the companyare not aligned with the current IT environment.

    Get your copy of this important guide today!

     

  • 2022 IBM i Marketplace Survey Results

    Fortra2022 marks the eighth edition of the IBM i Marketplace Survey Results. Each year, Fortra captures data on how businesses use the IBM i platform and the IT and cybersecurity initiatives it supports.

    Over the years, this survey has become a true industry benchmark, revealing to readers the trends that are shaping and driving the market and providing insight into what the future may bring for this technology.

  • Brunswick bowls a perfect 300 with LANSA!

    FortraBrunswick is the leader in bowling products, services, and industry expertise for the development and renovation of new and existing bowling centers and mixed-use recreation facilities across the entertainment industry. However, the lifeblood of Brunswick’s capital equipment business was running on a 15-year-old software application written in Visual Basic 6 (VB6) with a SQL Server back-end. The application was at the end of its life and needed to be replaced.
    With the help of Visual LANSA, they found an easy-to-use, long-term platform that enabled their team to collaborate, innovate, and integrate with existing systems and databases within a single platform.
    Read the case study to learn how they achieved success and increased the speed of development by 30% with Visual LANSA.

     

  • The Power of Coding in a Low-Code Solution

    LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed.
    Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

    • Discover the benefits of Low-code's quick application creation
    • Understand the differences in model-based and language-based Low-Code platforms
    • Explore the strengths of LANSA's Low-Code Solution to Low-Code’s biggest drawbacks

     

     

  • Why Migrate When You Can Modernize?

    LANSABusiness users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.
    In this white paper, you’ll learn how to think of these issues as opportunities rather than problems. We’ll explore motivations to migrate or modernize, their risks and considerations you should be aware of before embarking on a (migration or modernization) project.
    Lastly, we’ll discuss how modernizing IBM i applications with optimized business workflows, integration with other technologies and new mobile and web user interfaces will enable IT – and the business – to experience time-added value and much more.

     

  • UPDATED: Developer Kit: Making a Business Case for Modernization and Beyond

    Profound Logic Software, Inc.Having trouble getting management approval for modernization projects? The problem may be you're not speaking enough "business" to them.

    This Developer Kit provides you study-backed data and a ready-to-use business case template to help get your very next development project approved!

  • What to Do When Your AS/400 Talent Retires

    FortraIT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators is small.

    This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn:

    • Why IBM i skills depletion is a top concern
    • How leading organizations are coping
    • Where automation will make the biggest impact

     

  • Node.js on IBM i Webinar Series Pt. 2: Setting Up Your Development Tools

    Profound Logic Software, Inc.Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. In Part 2, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Attend this webinar to learn:

    • Different tools to develop Node.js applications on IBM i
    • Debugging Node.js
    • The basics of Git and tools to help those new to it
    • Using NodeRun.com as a pre-built development environment

     

     

  • Expert Tips for IBM i Security: Beyond the Basics

    SB PowerTech WC GenericIn this session, IBM i security expert Robin Tatam provides a quick recap of IBM i security basics and guides you through some advanced cybersecurity techniques that can help you take data protection to the next level. Robin will cover:

    • Reducing the risk posed by special authorities
    • Establishing object-level security
    • Overseeing user actions and data access

    Don't miss this chance to take your knowledge of IBM i security beyond the basics.

     

     

  • 5 IBM i Security Quick Wins

    SB PowerTech WC GenericIn today’s threat landscape, upper management is laser-focused on cybersecurity. You need to make progress in securing your systems—and make it fast.
    There’s no shortage of actions you could take, but what tactics will actually deliver the results you need? And how can you find a security strategy that fits your budget and time constraints?
    Join top IBM i security expert Robin Tatam as he outlines the five fastest and most impactful changes you can make to strengthen IBM i security this year.
    Your system didn’t become unsecure overnight and you won’t be able to turn it around overnight either. But quick wins are possible with IBM i security, and Robin Tatam will show you how to achieve them.

  • Security Bulletin: Malware Infection Discovered on IBM i Server!

    SB PowerTech WC GenericMalicious programs can bring entire businesses to their knees—and IBM i shops are not immune. It’s critical to grasp the true impact malware can have on IBM i and the network that connects to it. Attend this webinar to gain a thorough understanding of the relationships between:

    • Viruses, native objects, and the integrated file system (IFS)
    • Power Systems and Windows-based viruses and malware
    • PC-based anti-virus scanning versus native IBM i scanning

    There are a number of ways you can minimize your exposure to viruses. IBM i security expert Sandi Moore explains the facts, including how to ensure you're fully protected and compliant with regulations such as PCI.

     

     

  • Encryption on IBM i Simplified

    SB PowerTech WC GenericDB2 Field Procedures (FieldProcs) were introduced in IBM i 7.1 and have greatly simplified encryption, often without requiring any application changes. Now you can quickly encrypt sensitive data on the IBM i including PII, PCI, PHI data in your physical files and tables.
    Watch this webinar to learn how you can quickly implement encryption on the IBM i. During the webinar, security expert Robin Tatam will show you how to:

    • Use Field Procedures to automate encryption and decryption
    • Restrict and mask field level access by user or group
    • Meet compliance requirements with effective key management and audit trails

     

  • Lessons Learned from IBM i Cyber Attacks

    SB PowerTech WC GenericDespite the many options IBM has provided to protect your systems and data, many organizations still struggle to apply appropriate security controls.
    In this webinar, you'll get insight into how the criminals accessed these systems, the fallout from these attacks, and how the incidents could have been avoided by following security best practices.

    • Learn which security gaps cyber criminals love most
    • Find out how other IBM i organizations have fallen victim
    • Get the details on policies and processes you can implement to protect your organization, even when staff works from home

    You will learn the steps you can take to avoid the mistakes made in these examples, as well as other inadequate and misconfigured settings that put businesses at risk.

     

     

  • The Power of Coding in a Low-Code Solution

    SB PowerTech WC GenericWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed.
    Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

    • Discover the benefits of Low-code's quick application creation
    • Understand the differences in model-based and language-based Low-Code platforms
    • Explore the strengths of LANSA's Low-Code Solution to Low-Code’s biggest drawbacks

     

     

  • The Biggest Mistakes in IBM i Security

    SB Profound WC Generic The Biggest Mistakes in IBM i Security
    Here’s the harsh reality: cybersecurity pros have to get their jobs right every single day, while an attacker only has to succeed once to do incredible damage.
    Whether that’s thousands of exposed records, millions of dollars in fines and legal fees, or diminished share value, it’s easy to judge organizations that fall victim. IBM i enjoys an enviable reputation for security, but no system is impervious to mistakes.
    Join this webinar to learn about the biggest errors made when securing a Power Systems server.
    This knowledge is critical for ensuring integrity of your application data and preventing you from becoming the next Equifax. It’s also essential for complying with all formal regulations, including SOX, PCI, GDPR, and HIPAA
    Watch Now.

  • Comply in 5! Well, actually UNDER 5 minutes!!

    SB CYBRA PPL 5382

    TRY the one package that solves all your document design and printing challenges on all your platforms.

    Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product.

    Request your trial now!

  • Backup and Recovery on IBM i: Your Strategy for the Unexpected

    FortraRobot automates the routine tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:
    - Simplified backup procedures
    - Easy data encryption
    - Save media management
    - Guided restoration
    - Seamless product integration
    Make sure your data survives when catastrophe hits. Try the Robot Backup and Recovery Solution FREE for 30 days.

  • Manage IBM i Messages by Exception with Robot

    SB HelpSystems SC 5413Managing messages on your IBM i can be more than a full-time job if you have to do it manually. How can you be sure you won’t miss important system events?
    Automate your message center with the Robot Message Management Solution. Key features include:
    - Automated message management
    - Tailored notifications and automatic escalation
    - System-wide control of your IBM i partitions
    - Two-way system notifications from your mobile device
    - Seamless product integration
    Try the Robot Message Management Solution FREE for 30 days.

  • Easiest Way to Save Money? Stop Printing IBM i Reports

    FortraRobot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing.
    Manage your reports with the Robot Report Management Solution. Key features include:

    - Automated report distribution
    - View online without delay
    - Browser interface to make notes
    - Custom retention capabilities
    - Seamless product integration
    Rerun another report? Never again. Try the Robot Report Management Solution FREE for 30 days.

  • Hassle-Free IBM i Operations around the Clock

    SB HelpSystems SC 5413For over 30 years, Robot has been a leader in systems management for IBM i.
    Manage your job schedule with the Robot Job Scheduling Solution. Key features include:
    - Automated batch, interactive, and cross-platform scheduling
    - Event-driven dependency processing
    - Centralized monitoring and reporting
    - Audit log and ready-to-use reports
    - Seamless product integration
    Scale your software, not your staff. Try the Robot Job Scheduling Solution FREE for 30 days.