VB Notebook Main Page VB Notebook Files VB Notebook Function Library VB Notebook Articles VB Notebook Links VB Notebook

VBNotebook Home | Articles | Q&A Session: Common VB Newbie Questions

The following questions are often asked in VB related newsgroups. Most of these questions are answered in the help file or other documentation as well as many times on-line, However, they're asked over and over again anyway as new people take up Visual Basic.

__________________________________________________________________________________________

Q1: I want to control my VB app with command line arguments. (e.g. -? or /x). How can I get these arguments from the VB code?

A1: The Command$ or Command function does this. Here's an example...

    If UCase$(Command$) = "/TEST" Then
        frmTest.Show
    Else
        frmMain.Show
    End If

__________________________________________________________________________________________

Q2: I've written a DLL in VB and I've declared my functions in the General|Declarations area of my main program. When I try to run the program I get the following error message...

    Error Number: 453 Can't Find DLL Entry Point Test in C:\Windows\System\MyDLL.DLL

What am I doing wrong???

A2: VB only creates ActiveX DLLs. To work with ActiveX DLLs, you add a reference to the DLL from the References dialog and create an instance of the object using the Dim/Set/New method(early binding) or create it using the CreateObject function (late binding), you do NOT use the Declare statement. You can find additional information by consulting the help file, the books online sections dealing with component creation, and by studying the example programs provided with VB.

__________________________________________________________________________________________

Q3: I'm loading multiple lines of text from an ASCII file into a text box. The lines end with a Carriage Return/Linefeed pair, but in the text box all I see are two vertical lines where the CR/LF should be. What's wrong?

A3: Have you set the MultiLine property of the text box to True? Alternatively, are you sure that the file does contain a proper CR/LF pair and not something else?

__________________________________________________________________________________________

Q4: How do I create a list box with checks in it?

A4: Set the Style property of a list box to 1 - Checkbox

__________________________________________________________________________________________

Q5a: What's wrong with the BackColor property of the command button? I tried to change it but the color doesn't change.

Q5b: What's wrong with the Picture property of the command button? I assigned a picture to it but it doesn't show up.

A5: Set the Style property of a Command Button to 1 - Graphical

__________________________________________________________________________________________

Q6: I need for the EXE I create to have an icon associated with it and cannot figure how to do it. I have assigned an icon to my main/startup form but when the EXE is built it does not use it for the EXE. It uses the ugly default VB icon instead.

A6: You set this from the Project Properties Dialog's Make tab. In the Application frame you'll see a combo box that lists the forms in your application, select the form's icon you want to use.

__________________________________________________________________________________________

Q7: How do you make text boxes read-only in VB?

A7: Set the Locked property of the text box to True.

__________________________________________________________________________________________

Q8: I don't want users to be able to exit from a data entry form until they have completed all of the required fields and click OK to exit. However, they can click on the control box "X" and exit the form without entering the required data. How can I handle this situation?

A8: You can control the close button and the system menu's close item from the form's QueryUnload event. Here's a simple example...

    Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
        If UnloadMode = vbFormControlMenu Then
            If Not CheckRequiredFields() Then
                MsgBox "Please Complete All Required Fields.", vbOKOnly + vbExclamation
                Cancel = True
            End If
        End If
    End Sub

__________________________________________________________________________________________

Q9: How can I tell if a file already exists or not?

A9: In VB6 you can use the FileSystemObject's FileExists method to do this. This object also has several other useful file handling routines. Note that the FileSystemObject is slower than API methods although it is much simpler to use for novice level programmers.

In previous versions of VB you'll need to do this by writing a simple function that performs a file function and traps possible errors. Dir, Open, and GetAttr are common choices for this purpose. Note that you will need to trap and deal with potential error conditions anytime you're working with files. Here is an example function that uses the GetAttr function...

    Function IsFile(ByVal sFileName As String) As Boolean
    '
    ' Reports True if a file (sFileName) Exists, False if not
    '
        On Error GoTo IsFile_Err
        Dim nResult As Integer

        nResult = GetAttr(sFileName)
        If nResult = -1 Then
            IsFile = False
        Else
            IsFile = True
        End If
    Exit Function

    IsFile_Err:
        IsFile = False

End Function


There are also file related API calls that you can use if you need more detail, such as server rights info, or faster performance.

__________________________________________________________________________________________

Q10: How can I add icons to a VB6 application so that a user can choose from several of them when they create a shortcut?

A10: You place the icons in a resource file and load the resource file into your project. The icons will then be compiled into your application and visible to users.

__________________________________________________________________________________________

Q11: How do I select the existing text in a textbox when the user tabs into a textbox?

A11: You use the SelStart and SelLength properties of the text box in the GotFocus event. Here's an example...

    Private Sub txtBox_GotFocus()
        txtBox.SelStart = 0
        txtBox.SelLength = Len(txtBox.Text)
    End Sub

__________________________________________________________________________________________

Q12: How can I load and unload a control at runtime?

A12: If you're using VB6, you can use the Add method of the Controls collection. Note that there are some limits and restrictions on its use that you'll need to be familiar with in order to use it effectively. In some cases, the older control array method is preferable.

If you're using VB5 or earlier, a control array is the only way to dynamically load a control. A common way to do this is to have a zero member of the array on the form with it's visible property set to false. New controls in the array are created and destroyed, but the zero control remains an unused, unseen, template for these controls.

__________________________________________________________________________________________

Copyright 2000-2005, J. Frank Carr