Visual Basic Q&A

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘tab’

BUG: TextBox content is not automatically scrolled to display selected text on receiving focus

Symptoms
When you programmatically select the content of a text box, and the selected content resides in the area that you cannot see of the TEXTBOX control, then the text in the text box does not scroll automatically to display the selected text. Also, when you move to the TEXTBOX control by pressing TAB, the selected text does not appear automatically when the text box receives the focus.
Resolution
To work around this problem, you can use the ScrollToCaret method of the TEXTBOX control. This method scrolls the content of the control to the current caret position.
Copy the following code to the textBox1_Enter event:
Visual Basic Code

textBox1.ScrollToCaret()Visual C# Code

textBox1.ScrollToCaret();

BUG: Property Page of Remote Data Control Causes an Error

Symptoms
Pressing the ESC key while the cursor is in the SQL text box of theGeneral Tab in the property page of the Remote Data Control causes a blankGeneral tab. When you click the Colors tab and then click the General tabagain, an application error occurs and Visual Basic ends. The SQL text boxcan be empty or contain characters. Clicking the Cancel button on thewindow correctly closes the window. This behavior only occurs under theWindows 95 operating system.To work around this behavior, programmatically set the properties ofremote data control or use the Properties window of the Remote Data Objectto set the properties.
Resolution
Microsoft has confirmed this to be an issue in the Microsoft productslisted at the beginning of this article. Microsoft is researching thisissue and will post new information here in the Microsoft Knowledge Baseas it becomes available.

INFO: VB 6.0 Readme Part 14: Visual Component Manager

Symptoms
The information below includes the documentation and workarounds for VisualBasic 6.0. This information can also be found in the README.htm file thatships with Visual Basic 6.0 on the Visual Basic 6.0 CD-ROM. Please see theREFERENCES section of this article for a list of the Microsoft KnowledgeBase articles relating to the Visual Basic 6.0 readme.
Following is a list of all parts of the readme file:

Part 1.Important Issues – Please Read First!Part 2.Data Access Issues and DataBinding TipsPart 3.Control IssuesPart 4.Language IssuesPart 5.Samples IssuesPart 6.Wizard IssuesPart 7.Error Message IssuesPart 8.WebClass Designer IssuesPart 9.DHTML Page Designer IssuesPart 10. Extensibility issuesPart 11. Miscellaneous IssuesPart 12. Microsoft Transaction Server (MTS) IssuesPart 13. Dictionary ObjectPart 14. Visual Component ManagerPart 15. Application Performance Manager
Resolution
Known Problems in Visual Component Manager:
Related Files Tab (Component Properties Dialog Box)” Topic IncorrectVisual Component Manager User Interface Reference: The topic “Related FilesTab (Component Properties Dialog Box)” incorrectly states that the tab isused to display and enter files that are related to the selected component.In fact, none of the information displayed on this tab can be modified. Youcan add related files to a component only when publishing or re-publishingthe component. For more information, search online, with Search titles onlyselected, for “Publishing Components” in the MSDN Library Visual Studio 6.0documentation.
Removing Repository 1.0 Registry KeysIf you installed VCM 5.0 (previously available for Web download) you willhave the following Windows Registry keys setup. They were necessary for VCM5.0 and the 1.0 version of the Repository. If you find the followingRegistry entries then it is safe to remove them and may, in fact, improveVCM 6.0 performance:
HKEY_LOCAL_MACHINE\Software\Microsoft\Repository\CacheMaxAnnPropsHKEY_LOCAL_MACHINE\Software\Microsoft\Repository\CacheMaxObjectsHKEY_LOCAL_MACHINE\Software\Microsoft\Repository\CacheRelshipMaxCollectionsHKEY_LOCAL_MACHINE\Software\Microsoft\Repository\CacheRelshipMaxRowsHKEY_LOCAL_MACHINE\Software\Microsoft\Repository\MaxRowCacheAge
Adding Repository Tables to an Existing .mdb FileIf you try to open an existing .mdb file from within VCM that is not arepository database (i.e., it does not contain the repositorystructure/tables), you will be asked if you want the repository tablesadded to the database. You should not do this for normal use; therepository should generally be in a separate database. This will work, butit can take as long as 10 minutes to create the repository structure in anexisting .mdb file.
To create a brand new .mdb file containing the repository structure, right-click in the folder outline, click Repository, click New, and then enterthe name of the file you want to create.

INFO: VB 6.0 Readme Part 10: Extensibility Issues

Symptoms
The information below includes the documentation and workarounds for VisualBasic 6.0. This information can also be found in the README.htm file thatships with Visual Basic 6.0 on the Visual Basic 6.0 CD-ROM. Please see theREFERENCES section of this article for a list of the Microsoft KnowledgeBase articles relating to the Visual Basic 6.0 readme.
Following is a list of all parts of the readme file:

Part 1.Important Issues – Please Read First!Part 2.Data Access Issues and DataBinding TipsPart 3.Control IssuesPart 4.Language IssuesPart 5.Samples IssuesPart 6.Wizard IssuesPart 7.Error Message IssuesPart 8.WebClass Designer IssuesPart 9.DHTML Page Designer IssuesPart 10. Extensibility issuesPart 11. Miscellaneous IssuesPart 12. Microsoft Transaction Server (MTS) IssuesPart 13. Dictionary ObjectPart 14. Visual Component ManagerPart 15. Application Performance Manager
Resolution
“Command-Line Safe” Add-In BehaviorYou can use the Load Behavior box in the Add-In Manager to control how andwhen an add-in loads in Visual Basic:
Loaded/Unloaded — either loads or unloads a selected add-in when thebox is checked or unchecked.Load On Startup — indicates whether the selected add-in should loadwhen the Visual Basic IDE is started.Command Line — indicates whether an add-in should load when VisualBasic is started from a command line, either through a DOS prompt or ascript.When you select Command Line load behavior for an add-in, you may get thefollowing warning message:

“The selected add-in has not been confirmed to be ‘command-line safe’,and may require some user intervention (possible UI). Do you wish toproceed?”
This occurs when you select an add-in for Command Line load behavior thatwas not declared by the author of the add-in to be “command line safe” whenit was created. (This can be indicated with the Add-In Designer through acheckbox.)
“Command-line safe” means that the add-in is registered in a way toindicate that it contains no user interfaces that require user input whenVisual Basic is invoked through a command-line. A user interface caninterfere with the operation of unattended processes (such as buildscripts).
If you don’t indicate that an add-in is command-line safe (even if it iscommand-line safe), when a user selects your add-in and then Command Linein the Load Behavior box, they’ll receive the warning message. This isn’t aserious problem, but merely a warning to the user that the selected add-inmight possibly contain UI elements that can pop up unexpectedly and halttheir automated scripts by pausing for user input.
Manually Setting Add-In Registry ValuesYou can also manually set the command-line safe flag (as well as the othervalues) for an add-in through the Windows registry.
NOTE: You should not attempt to directly manipulate any Windows registryentries unless you are familiar with doing so. Setting an invalid registryentry can cause problems with Windows, even preventing you from being ableto load Windows.
In Visual Basic 6.0, the key that holds add-in information is located in:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Visual Basic\6.0\Addins\<add-in.name>.
For Visual Basic 6.0, the LoadBehavior DWord values are:
None = 0Startup = 1Command Line = 4Command Line / Startup = 5There is also an additional DWord value that indicates whether the add-inis command-line safe: CommandLineSafe. A value of 1 indicates the add-in iscommand-line safe, while a value of 0 (the default) indicates that it isnot command-line safe. A value of 0 is implied if you forget to check thecommand-line safe box in the Add-In Designer since the default value of 0is assumed, and the add-in isn’t considered command-line safe.
So, to demonstrate how to use these values to indicate that a fictitiousadd-in (My.Addin) is command-line safe and to have it load when VisualBasic is started by command-line, you would set the following registryvalues, using a tool such as RegEdit:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Visual Basic\6.0\Addins\My.Addin

“FriendlyName”=”A friendly name for your add-in”"Description”=”This value describes the add-in”"LoadBehavior”=dword:4″CommandLineSafe”=dword:1
Using the Add-In DesignerVisual Basic 6.0 includes a new tool, called the Add-In designer, to aidyou in creating add-ins. To open it:
Create a new add-in project.In the Project Explorer, under Designers, is a designer called Connect.Double-click it to activate the Add-In designer.Unfortunately, context-sensitive help currently isn’t available for the Add-In designer. Help topics are available, however. You can find theappropriate topics by searching for Add-In Designer in the MSDN index. Youshould see a list of three associated topics:
“Using the Add-In Designer”"General Tab (Add-In Designer)”"Advanced Tab (Add-In Designer)”For more information, search online, with Search titles only selected, for”Registering Add-Ins” in the MSDN Library Visual Studio 6.0 documentation.
Add-In Designer: More Information About Specifying Satellite DLLWhen creating an add-in with the Add-In designer, you can specify a DLL onthe Advanced tab. Be sure, however, to type only the name of the DLL file,and not its fully-qualified path. For example:
MyAddinName.DLL
not:
Addins\MyAddinName\MyAddinName.DLL
Localized Satellite DLLsIf you create a localized satellite DLL, you should also create a resourcesdirectory and a locale ID directory for the satellite DLL and install theDLL in the appropriate directory. The schematic for such a path is:
<AddIn Directory>\Resources\<Locale ID>\<MySatellite.DLL>
For example, a satellite DLL for the German version (Locale ID = 1031)would go into the directory:
C:\Program Files\MyAddin\Resources\1031\MyAddinName.DLL

How To Log On to a Terminal Server Session Programmatically from Visual Basic

Symptoms
The Terminal Services ActiveX client control does not expose the ImsTscNonScriptable interface. However, this interface can be used to configure automatic log on for a Terminal Services Session programmatically, which enables the programmer to log a user on to a Terminal Services Session without receiving the Windows Logon prompt. This is demonstrated in the sample in the “More Information” section.
Resolution
To run this program, configure your Terminal Server computer as follows: Log on to the Terminal Server locally as an administrator.On the Start button, click Programs, click Administrative Tools, and then click Terminal Services Configuration.Click on Connections.In the right pane, right-click RDP-Tcp, and then choose Properties.Click on the Logon Settings tab.Deselect Always prompt for password, and then click OK.NOTE: For security reasons, Microsoft recommends that you do not implement this scenario without extreme care and a clear understanding of Microsoft Windows security.
Sample CodeStart a new Standard EXE project. Form1 is created by default.On the Project menu, click to select Components, select Microsoft Terminal Services Control(redist), and then click OK. If this control is not available, see the “References” section of this article for information on how to download and install it.Add one Terminal Services Control to Form1, making sure it is big enough to handle the display of the session.Add three Label controls, three TextBox controls, and one CommandButton control to Form1. Make sure that Lable1 and Text1 are on the same line, and that Label2, Text2, Label3, and Text3 are on the same line.Paste the following code into the General Declarations of Form1:

‘ This code only works when you set the configuration on the Server-side.’ Log on to the Terminal Server as an administrator’ Start\Programs\Administrative Tools\Terminal Services Configuration’ Click on Connections’ On the Right Pane, right-click on RDP-Tcp and choose Properties’ Click on the “Logon Settings” Tab’ Uncheck “Always prompt for password” and click OKOption ExplicitPrivate Obj As IMsTscNonScriptablePrivate Sub Form_Load()Text1.Text = “”Text2.Text = “”Text3.Text = “”Label1.Caption = “Server”Label2.Caption = “UserName”Label3.Caption = “Password”Command1.Caption = “Connect”Text3.PasswordChar = “*”End SubPrivate Sub Command1_Click()Set Obj = MsTscAx1.ObjectMsTscAx1.Server = Text1.TextMsTscAx1.UserName = Text2.TextObj.ClearTextPassword = Text3.TextMsTscAx1.ConnectEnd Sub Save the project, press the F5 key to run it, and note that after you supply your username, password, and server name, you are not prompted for a logon screen at the server. Microsoft recommends that you enlarge the Terminal Server .ocx file so that you are able to manipulate the Shut Down dialog box.

How To Extract Multiple RDO Resultsets from Stored Procedures

Symptoms
SQL Server stored procedures are capable of returning more than one recordset and the Remote Data Object (RDO) has the ability to access these Multiple Resultsets.
When calling these stored procedures, the following error can be encountered:

Error 40002
“37000: [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open a cursor on a stored procedure that has anything other than a single
select statement in it” The following code sample showing how to return the multiple Resultsets using the MoreResults Property of the RDO.
Resolution
There are two methods to avoid Error 40002.
Method 1 Use The ODBC Cursor library rather than Server Side Cursors. To do this, use the following code:

rdoEngine. rdoDefaultCursorDriver = rdUseODBC
- or -

rdoEnvironments(0).CursorDriver = rdUseOdbc This option gives better performance for small result sets, but may degrade quickly for larger result sets depending on the Server and workstation configuration.
NOTE: If you are using SQL text fields, you must use Server-side cursors to bypass Error 40002″

Method 2 Use Server Side Cursors, a Forward Only Cursor, and a rowset size of 1. Make the server create a cursor-less resultset on the server side by using a forward only cursor and a RowSetSize of 1.
The following code sample illustrates how to create a stored procedure that returns multiple result sets using method 2. Create the stored procedure on SQL Server:
Start the ISQL utility that shipped with SQL server. Connect to your SQL server. Select the pubs database from the combo box labeled DB. Enter the following lines into the Query Tab:

CREATE PROCEDURE TestMultiResults ASselect * from authorsselect * from discountsGO Choose the Query|Execute menu. The Results tab should display:
This command did not return data, and it did not return any rows
A stored procedure called TestMultiResults has now been created in the pubs database on SQL Server. Create the Visual Basic client to call the stored procedure:
Start Visual Basic. Form1 is created by default. Add a Command Button (Command1) and a List Box (List1) to Form1. Add the following code to Form1:

Private Sub Form_Load()Command1.Caption = “Run Stored Procedure”End SubPrivate Sub Command1_Click()Dim cn As rdoConnectionDim ps As rdoPreparedStatementDim rs As rdoResultsetDim strConnect As String’set cursor driver to use server-side cursorsrdoDefaultCursorDriver = rdUseServer’open a connection to the pubs database using DSNless connections’Remember to change the following connection string parameters to reflect the correct valuesstrConnect = “Driver={SQL Server}; Server=myServer; ” & _”Database=pubs; Uid=<username>; Pwd=<strong password>”Set cn = rdoEnvironments(0).OpenConnection(dsName:=”", _Prompt:=rdDriverNoPrompt, _ReadOnly:=False, _Connect:=strConnect)’create a prep stmt for the stored proc callSet ps = cn.CreatePreparedStatement(“MyPs”, _”{call TestMultiResults}”)’set the RowSet size to 1ps.RowsetSize = 1′open the resultset with forward-only cursorSet rs = ps.OpenResultset(rdOpenForwardOnly)’add the first resultset to a list boxWhile Not rs.EOFlist1.AddItem rs(“au_fname”) & ” ” & rs(“au_lname”)rs.MoveNextWend’move to the second resultsetrs.MoreResultslist1.AddItem “Second Resultset Below”‘add the second resultset to the same list boxWhile Not rs.EOFlist1.AddItem rs(“discounttype”) & ” = ” & rs(“discount”)rs.MoveNextWend’Close the resultset and the connection and set both to nothingrs.CloseSet rs = Nothingcn.CloseSet cn = NothingEnd Sub Run the project and click the “Run Stored Procedure” button. You should see a list of Authors and then Discounts in the list box.