K & K Consulting    K & K Consulting

K&K Home VB Guru Home Search VB Site VB Code VB Tips VB Tutorials VB Questions

K&K Home


Answered Questions For Jan/Feb 2002

A Note From The Guru

Date:  Feb 23 2002

The Guru has been away for a week or so checking out job prospects, during that time, I received about 50 questions . I am sorry, I just do not have time to answer all of those questions, so I have picked out a few and will only answer them.

I apologize to all those that did not receive an answer. The ones I did answer are below

ActiveX In HTML ?

From:  Wim Baars
Date:  Feb 23 2002

hi there, i have made a activeX control in VB6, and integrated the control in HTML. I put an ADO control in that activex and point to a ODBC DSN for a connectionstring. In the recordsource i put a SQL select statement to a table. This is working fine on the local server. But when i put these on the server i can't get data from the servers database in this control. When i use the name of the local DSN i see the data from the local database again. Can you tell me what the problem is. (sorry for my poor english)!

The Guru's Response:

Hi Wim,

I have had this problem before with displaying data in ActiveX controls, it was exactly like you described, worked fine on the local machine and it worked great for any user that was in my domain, out of the domain, nothing would display.  I am really not too sure how it is working with your custom made control, but I have a suspicion that the Domain issue is causing you the same problems, I would suggest you try and convert the control to DHTML and see if that works

I hope This Helps


Percentage ?

From:  Anthony Ference
Date:  Feb 23 2002

What is the coding for percentages in Visual Basic?

The Guru's Response:

Hi Anthony,

The FormatPercent function formats a number as a percentage, including two decimal places by default. For example

Msgbox FormatPercent(4/5)

Will display 80.00% in a message box

I hope This Helps

Resource Files?

From:  Amir Jafri
Date:  Feb 23 2002

Hi, I'm using resource files in my VB app to support multiple languages. My initial impression was that i could change the resource file without having to recompile the app(and the MSDN help hints at this). Though I have found that I have to make a new .exe each time. The idea is that a few months from now if i want to add support for another language, how would i do it? Right now i have added a resource file to my project and am using LoadResString(..) to extract the relevant string from my string table. Could any1 tell me if there is a way to update the string table (add columns/languages or change the strings) without having to recompile the code. Thanks, Amir

The Guru's Response:

Hi Amir,

Resource files are great for doing exactly what you have just mentioned, adding different language support and also for custom dialog messages, the only fall back is that the Application must be recompiled every time you want to add or remove resources from your EXE.  Now, I did see what you were talking about in MSDN, but I think, I am not positive, that the Add/Delete Resources on the fly are only for VB.Net and.....it does not change the final EXE, it only changes the resources in memory. So, if you were to load an application with resources, changed the resources while the application was running, then close the application, the recourses would go back to the original as compiled.

The only other way to dynamically make changes to languages and such is to place it all in a INI file or in the registry.

I hope this helps

Banner 10000077

Form Counter?

From:  manu kapoor
Date:  Feb 23 2002

how to write a program in VB which can calculate number of forms in the same application and number of controls in each form of that applicaion ?

The Guru's Response:

Hi Manu,

You can access the number of forms in your application by way of the Global VB variable (it is built into VB), Forms.  The Forms Variable has no documentation in MSDN and almost nothing in the Object Browser, but it acts just like a regular form and you can do a tremendous amount of things with it.  Here is an example of how you will use it.

Create a new, Standard EXE project in VB.  Add 2 more forms to the project to that you have three forms in total.  Next, add a few controls to each form, it really does not matter what type of controls they are.  Next, paste the following code into your Form1_Load event:

Private Sub Form_Load()

Dim FormCount As Long
Dim Counter As Long
Dim MsgString As String


FormCount = Forms.Count

For Counter = 0 To FormCount - 1
MsgString = MsgString & " Form " & Forms(Counter).Name & " Has " & Forms(Counter).Controls.Count & " Controls On it" & vbCrLf

MsgBox MsgString

End Sub

I hope this helps

Word Mail Merge?

From:  Andreas Fecker
Date:  Feb 12 2002

--- Winword ---- Mailmerge ---- missing datasource ---- I have to open a Worddocument with something like: wordapp.documents.open(File) in order to replace the path in .mailmerge.datasource.name this leads to an error when the original datasource isn´t available, because all the documents were copied from one server to another (Office 97 worddoc use the UNC path of old fileserver) any ideas

The Guru's Response:

Hi Andreas,

I am sorry, I have not done OLE Automation with Word in quite a long time.  I did a quick search and could not find any references to your problem.  I can not help you with this.

If any one else knows how to solve this problem for Andreas, please email and I will post it here.


50% off with purchase: ongoing

Dynamic DataReports?

From:  Asim Khan
Date:  Feb 10 2002

Hi I have a datareport that im displaying using a standard recordset. Set DataReport1.DataSource = rst the problem that im having is that my user wants me to display information about a field. The field can contain three values L, O or R. He wants to know what percentage of the records are L's and so on. In code I can loop through the recordset and count the instances of L's etc. But how do I display this number in the data report? Thanks Asim

The Guru's Response:

Hi Asim,

First and foremost, I would like to mention that the DataReport in VB is very unstable and prone to crash; if you insist on using it, this is how you would do it:

Within the DataReport there are Sections, within the Sections are Controls, each of these become properties of the parent object.  So, lets say you place a Label called Label1 (DUH) in the details section of your data report, you can change the Caption of that label with the following code:

DR1.Sections(3).Controls("Label1").Caption = "Test"

So, basically, all you have to do is place a Label on your report and do not bind it, do your calculations, then change your Label's caption to your calculation.

I hope this helps

Recordset Error?

From:  Steven Broos
Date:  Feb 10 2002


I have a very urgent question, for a project in school.

I want to write a function in visual basic 6, and the function should be located in a module-file (.bas) It should return a recordset from an SQL database. SO if I need any data I can just do exec_SQL(sql-statement) This is what I've done:

Dim strConn As String

Function Sql_Exec(Sql As String) As ADODB.Recordset
   strConn = "Driver={SQL Server};Server=(local);Database=E3;Uid=sa;Pwd=;"

   Dim objDB As New ADODB.Connection
   Dim DB As New ADODB.Recordset

   objDB.Open strConn
   Sql_Exec = objDB.Execute(Sql)

   Set DB = Nothing
End Function

in my form:
Dim Sql As String 'Om de query in te steken
Dim DB As New ADODB.Recordset 'Recordset om te teruggekregen waarden
in te steken

Sql = "SELECT * FROM tbl_User WHERE vchUserLogin='" & Me.txtLogin.Text & "' AND vchUserPasswd='" & Me.txtPass.Text & "';"
Db = Sql_Exec (Sql)

Here is the error.
Db = Sql_Exec (Sql) --> THis is marked yellow, and I get an error 'invalid use of property'. Do you have any idea what i can do to make it work ?

Best Regards,

Steven Broos

The Guru's Response:

Hi Steve,

In VB, you have a few different types of variables; you have the regular types like String, Long and the like.  You all so have variables known as OBJECTS, these are any type of variable that are derived from a class; a recordset is an OBJECT Type.  As such, you must follow the object type rules when you are initializing them, usually with the Set keyword, for example:

Set MyObject = Createobject("MyDll.MyClass")

And when you are finished with the Object, you always release it with the Nothing statement, for example:

Set MyObject = nothing.

Now, having said all of that, your problem is that you are not using the SET Keyword in the line that is causing you the problem; I ran the code and you also have the same error in the:

Sql_Exec = objDB.Execute(Sql)

Here is how your code should now look:

Dim strConn As String

Function Sql_Exec(Sql As String) As ADODB.Recordset
   strConn = "Driver={SQL Server};Server=(local);Database=E3;Uid=sa;Pwd=;"

   Dim objDB As ADODB.Connection
   Set objDB = New ADODB.Connection  ' Notice I re-wrote this line, it does not use the "Dim Varname as New" Read my Binding Tutorial to find out why
   objDB.Open strConn
Set Sql_Exec = objDB.Execute(Sql) ' Notice I changed this to Set

   Set DB = Nothing  ' You do not need this line, remove it
End Function

in my form:
Dim Sql As String 'Om de query in te steken
Dim DB As ADODB.Recordset

DB = New ADODB.Recordset  ' Notice I re-wrote this line, it does not use the "Dim Varname as New" Read my Binding Tutorial to find out why

Sql = "SELECT * FROM tbl_User WHERE vchUserLogin='" & Me.txtLogin.Text & "' AND vchUserPasswd='" & Me.txtPass.Text & "';"
Set Db = Sql_Exec (Sql) ' Notice I changed this to Set

I hope this helps



From:  CrashOveride
Date:  Feb 06 2002

Im a new student for a VB subject.hmm ive learned a bit of VB.I'm currently using a VB 6.0.hmm i have a problem in doing printing.hmmm let say that i have an input into my database(MS Access).The input is using a form.Could you help me by giving me the code so that i can print some part of the form without printing the command button along?Could u reply this as soon as possible coz i need it by this Friday 8th,february,2002.I have to hand out my tesis by this Satu! rday..Pleaasee i'm really desperate.

The Guru's Response:

Hi Crash,

It may be a language thing, but I am sorry, I really have know idea what you are asking me for.  What exactly do you want to be printed out? I tried to email to ask you this, but the email address you supplied came back as inactive.

If you just want to print out the contents of a textbox or other control, you could so something similar to the following:

Printer.Print Text1.text

I hope this helps

Find A Window?

From:  mmikem
Date:  Feb 06 2002

How do I find a specific window in VB, based on the window title?

The Guru's Response:

That is one way, the other way is to call the EnumWindows() API. The EnumWindows(), because it uses Call Back Functions, is beyond the scope of the Q&A section; I will focus on the Findwindow() API call, this uses the Window Title.

API Call


FindWindow() The FindWindow function retrieves the handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows.

Here is an example:


Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Form_Load()
Dim RetVal As Long
Dim WinName As String

WinName = InputBox("Enter the exact window title:" & vbCrLf & "Note: must be an exact match")

RetVal = FindWindow(vbNullString, WinName)

If RetVal = 0 Then
MsgBox "Sorry, I could not find that window"
MsgBox "The Handle to that Window is: " & RetVal
End If

End Sub

I hope this helps.

Winner 468x60

VB & Unicode?

From:  prof.Sorin Olteanu
Date:  Feb 02 2002

I am a medium-level amateur programmer in VB5 under Windows98. I want to create an application for myself (eventually for other classicists also) and I need to know: 1.How could I make common controls (especially PictureBox, Grid and TextBox) display Unicode characters - in order to use greek and sanskrit alphabets? MsForms 2.0 controls are of no help for me: they have no PictureBox and Grid, and the existing controls miss a lot of the properties I need. Could you explain to me this procedure and, if possible, give me a sample code? 2. Can I use Unicode characters on controls created with API functions (CreateWindowEx) or even on common controls wich have a hWnd?

The Guru's Response:

Hi Prof Sorin,

OK, this is how the various applications and systems work and how they hold/manipulate strings:

Application/Operating System

How It Handles Strings

Visual Basic 5/6 Visual Basic uses Unicode to store and manipulate strings
Windows 95/98/ME Uses ANSI or DBCS to store and manipulate strings.
Windows NT/2000/XP Uses Unicode to store and manipulate strings.
Automation in Windows 95/98/ME & NT/2000/XP Uses Unicode to pass the strings back and forth.
Windows 95/98/ME API calls Uses ANSI
Windows NT/2000/XP API calls Uses Unicode

Now, there are exceptions to the rules, any API call with an “A” on the end means it is an ANSI function call and any string passed to it will be treated as such. Any API call with a “W” on the end means it is a “Wide” call and the strings passed will be treated and manipulated as Unicode. Any call without either an “A” or a “W” will be treated as the operating system dictates. (Win 95/98/ME = ANSI, Win NT/2000/XP = Unicode)

Now, I have not used Greek or Sanskrit alphabets, but the text within VB and the controls are being stored as Unicode, no matter what OS you are using. I think the problem you are having stems from the type of font you are using. The default font for a control is MS Sans Serif, a non-True Type font with only one script, Western. If you change the font to something like Time New Roman, and then change the “script” property, you are open to all sorts of character sets, for example, Greek, Arabic, Turkish, Cyrillic and a few more.

If this still does not work, let me know, there are API calls that may do it, but you would have get the handle to the control, get the text, call the API to “Paste” it into a control and I think that is a lot of over head.

I hope this helps.

Sub Classing For Unicode?

From:  prof.Sorin Olteanu
Date:  Feb 02 2002

I have a second problem and I ask again for your help (am I abusing?): 1. I need to make typing manager using Unicode characters (for ancient greek, polytonic). I learned how to set a general hook to intercept keystrokes, by subclassing, but I don't understand this: after processing the keyboard stroke, how do I send it back to the current application and how can I modify the text already edited? Could you also give me a short sample code (VB5 under Windows98)?

The Guru's Response:

I really do not think you will need to do the sub classing, when you read my response to your first question, you will see that by changing the font properties, the control will automatically display Greek characters.

One more thing, instead of doing all of that sub classing, you could have just written code in the KeyDown event of the control, this event tells you the key that was pressed and a bitmask telling you other keys as well, for example, if the user also pressed the Shift or Ctrl key along with the character key. You can also directly manipulate the text entered or even existing right from this event.

Note: I did not recommend the KeyPress event because it only fires when the user presses an ANSI key on the keyboard and only passes the ANSI key code.

I hope this helps

Has Windows Finished Loading Yet?

From:  Jim diGriz
Date:  Jan 31 2002

Hi. Just tried to post a question in your VB Questions section, and the site seemed to bomb out when I clicked submit, so I thought I'd submit my question this way! :-)

I have written a program that allows you to choose from more display modes than windows XP makes available. It runs on startup by putting a link to it in the registry key "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run" but it seems to load before windows has finished loading user settings, which causes a problem when the program tries to change display modes

Do you know of any way I can make my program wait for WinXP to finish loading the user settings etc.(and make sure the desktop is displayed) before it tries to do any mode changes?


Jim diGriz

The Guru's Response:

Hi Jim,

First of all, I want to apologize for the question page crapping out, Tripod has done something to their site and I have had problems all over the place, from formatting changes and URL name changes to pages just disappearing.  Any page that submits data is severely effected, so I have written some scripts on the page to correct it, for now anyway.

OK, now on to your question. This was a tuff one, but I think I may have your answer. I started looking for an API that would tell me if Windows had finished loading, unfortunately, there is no such call (documented anyway). So I tried a different angle, I found an API call that returns the handle of the desktop, if it finds the desktop it returns a non-zero number, if it fails (the desktop is not loaded yet) it returns 0. I tried it and it seems to work OK, so here you go…

Paste this API call into the declares section of your form or BAS file:

Private Declare Function GetDesktopWindow Lib "user32.dll" () As Long
'GetDesktopWindow returns a handle to the desktop window. The desktop window is the window that makes up the desktop of the computer -- that is, the screen. If the function fails, it will return 0 instead of the handle.

Next, past this code, or modify it to your needs into the form load Event or Sub Main call:

' Find the device context of the desktop
Dim deskhwnd As Long ' handle to the desktop

Do While deskhwnd = 0
deskhwnd = GetDesktopWindow() ' get the desktop's handle

I hope this helped




From:  Mohamed Shifaz
Date:  Jan 29 2002

Can you please tell me how to make program for MS-DOS using VB.

The Guru's Response:

Sorry Mohamed, you can not use the VB compiler to create MS DOS applications. There are a few reasons for this, for one, DOS is a 16 bit operating system and VB 4,5 and 6 are 32 bit compilers.  The next reason is DOS programming and Windows programming are two completely different styles.  In DOS programming it is called procedural, you define a main program loop and keep checking for user input, branching off when the user enters a correct response.  In Windows programming, it is called event development.  The operating system will keep the program alive as long as the program keeps checking it message queue for events. The operating system will place events (mouse clicks or mouse movements) into the the programs event queue.  As long as you have defined code to handle that event, that events code will run.

So, as you can see, not only are the programming methods different, but a Windows compiler is completely different from a DOS compiler

Data Providers?

From:  Jola Carter
Date:  Jan 29 2002


I read your tutorial on UDL and I am very interested if there is any way of adding a Provider in Data link properties. I am interested in linking KDB database.

Please help if you know how to do it.


The Guru's Response:

Hi Jolanta,
The only way to add a provider is to install an OLE DB or ODBC type driver for that database to your system. Microsoft is the best place to start looking to see if there is a driver available for your database, as they are the ones that write most of them, including databases like Oracle. To point you in the right direction, here is the link to the Microsoft Universal Data Access Web Site.

I hope this helps

Assess 468x60

Shareware Protection

From:  Tim Gravlin
Date:  Jan 29 2002


I have recently finished a program that will be distributed over the internet in a shareware like fashion. I was wondering if you know any good ways to code for the 30 day trial period expire, and to create a key generator if they call and purchase the program??

if you have any ideas i would love to hear them..



The Guru's Response:

Hey Tim,
The best way to do the 30 day trial period is to create some obscure registry setting name with 2 keys, Date (the installation date) and CD Key (the license number).  When your setup application installs your app, have it check for this registry setting. If it is all ready there, check the values, if it is not registered (only the default CD key) and more then 30 days are up, do not let the setup continue, or have the setup ask for a valid key (one they bought from you) before it will continue.  Also have your application check these values every time it starts up, if 30 days are gone by, then do not let it start.  Have your setup program leave the registry entries when the user un-installs your app, this way if 30 days have passed and they try to un-install and then re-install the app again, it will still not work.  The only way they can get your application to re-install again is to either buy the program or scrub their computer.  But make sure the user would never know the registry settings are for your program, even if they were to search the registry manually, or else they will be able to roll back the date field them selves.

A key generator is a little more difficult, it must be an algorithm that is decided upon for both the application and your generator.  How I would do it is like this,  decide how many characters (numbers and letters) the key should be first, then decide if the first char should be a letter or a number.  That fist letter or number can only be within a certain range say a,b or c for letters or 7,8 or 9 for numbers.  The next char can only be a certain letter or number depending on the last letter or number, then repeat.

So, say you decide the first char can only be a letter between A and D and the second char is a number, if the user enters A, then the second char can only be a 6, B then only a 9 and keep repeating this algorithm until you reach the maximum number of chars in the CD key.

As I said, this is how I would do it, it probably is not the be the best way of doing it, I would poke around the Net to see if their are more examples.

I hope this helps

Send mail to WebMaster with questions or comments about this web site.
This website is best viewed with a screen resolution of 800*600 or better.
This website is optimized for Microsoft Internet Explorer 6.x
K&K Consulting, Proud to be a Microsoft Business Partner.
Last modified: January 31, 2002