Search Tools Links Login

Frequently Asked VB Questions


Visual Basic 6, or VB Classic

If you've ever been to the Visual Basic Discussion Forum then you realize why I'm posting this. It's simply a list of commonly asked questions. Please feel free to add on anything that either you've learned here at PSC or anything that you find yourself answering on a regular basis. (Revised Jul 06, 2001)
Revised Jul 19, 2001
Revised Jul 24, 2001

Original Author: Sean Street

Code















































































































Functionality
Relative Code
Related Links
Writing/Appending text to a text file

Open "C:MyTextFile.txt" For Output As #1
Open "C:MyTextFile.txt" For Append As #1

Input/Output Text file
Reading text from a text file

Open "C:MyTextFile.txt" For Input As #1

Input/Output Text file
Setting a string to the application directory

strFileName = App.Path & (Trim(Chr(32 - (60 * (Asc(Right(App.Path, 1)) <> 92)))))

Relative paths
Reading data from an INI file

Private Declare Function GetPrivateProfileString Lib "kernel32"_
Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any,_
ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long,_
ByVal lpFileName As String) As Long



Public Function GetINIData(ByVal strParent As String, strKey As String) As String

  Dim strBuffer As String

  Dim strFilename As String


  strBuffer = Space(145)

  strFileName = App.Path & (Trim(Chr(32 - (60 * (Asc(Right(App.Path, 1)) <> 92))))) & "MyINI.INI"


  GetPrivateProfileString strParent, strKey, "", strBuffer, Len(strBuffer) - 1, strFilename

  GetINIData = Left(strBuffer, InStr(strBuffer, Chr(0)) - 1)

End Function


INI file template routines
Writing data to an INI file

Private Declare Function WritePrivateProfileString Lib "kernel32"_

Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String,_

ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long



Public Sub WriteINIData(ByVal strParent As String, strKey As String, strValue As String)

  Dim strFilename As String


  strFileName = App.Path & (Trim(Chr(32 - (60 * (Asc(Right(App.Path, 1)) <> 92))))) & "MyINI.INI"


  WritePrivateProfileString strParent, strKey, strValue, strFilename

End Sub


INI file template routines
Dynamically adding controls

Rem This code is for Visual Basic 6 only but the second link shows how to do it with VB4/5

Private Sub Form_Load()

Form1.Controls.Add "VB.CommandButton", "cmdMyButton"

With Form1!cmdMyButton

.Visible = True

.Width = 2000

.Caption = "Dynamic Button"

End With

End Sub


Dynamically create a control(VB6)


Creating controls dynamically (VB6,5 and 4)
Adding items to a combo/list box and
setting it to the first items if an item exist

cmbMyComboBox.AddItem "Item1"

cmbMyComboBox.ListIndex = (cmbMyComboBox.ListCount=0)

None
Having problems with the license of your Winsock control?

Just go to the link

Register/License Winsock Control
Allows only numeric characters in a textbox

Private Sub txtNumbersOnly_KeyPress(KeyAscii As Integer)

  KeyAscii = KeyAscii * Abs(((KeyAscii > 47 And KeyAscii < 58) Or KeyAscii = vbKeyBack))

End Sub


Masking Control
Prints a picture control contents to the printer

Printer.PaintPicture picMyPictureControl.Picture, 1, 1

Printing picture control contents
Copy picture/text to the Clipboard

Clipboard.Clear
Clipboard.SetData picMyPictureControl.Picture 'Used for pictures

Clipboard.SetText txtMyTextBox.Text 'Used for text


Copying contents to the Clipboard
Paste picture/text from the Clipboard

picMyPictureControl.Picture = Clipboard.GetData 'Used for pictures

txtMyTextBox.Text = Clipboard.GetText 'Used for text


Pasting contents from the Clipboard
Evaluate resposes from MsgBox

Rem Use this to check before you save; used with yes/no or ok/cancel options

If MsgBox("Are you sure you want to save thses changes?", vbQuestion + vbYesNo, "Save?") = vbNo Then Exit Sub





Rem You can use this to check before you exit; used with yes/no/cancel or abort/retry/ignore

Select Case MsgBox("Would you like to save before you exit?", vbQuestion + vbYesNoCancel, "Exiting")

Case vbYes

Rem Save it then quit

Case vbNo

Rem Quit

Case vbCancel

Exit Sub

End Select



None
Read data from an Excel spreadsheet

Dim xlsApplication As Object

Dim lngRowCount As Long

Dim intColCount As Integer

Dim blnBlankRow As Boolean

Dim strValue As String



Set xlsApplication = CreateObject("Excel.Application")



xlsApplication.Workbooks.Open "C:Test.XLS"



For lngRowCount = 1 To 65536

blnBlankRow = True

For intColCount = 1 To 255

strValue = xlsApplication.Cells(lngRowCount, intColCount).Value

Rem Set this value into your table/field

If Len(strValue) > 0 Then blnBlankRow = False

Next intColCount

If blnBlankRow Then Exit For

Next lngRowCount



xlsApplication.Workbooks(1).Close savechanges:=False

xlsApplication.Quit



None
Read data from Outlook Inbox/SentMail folders

Dim outApplication As Object

Dim outInBox As Object

Dim outOutBox As Object



Set outApplication = CreateObject("Outlook.Application")



Set outInBox = outApplication.GetNamespace("MAPI").GetDefaultFolder(6)

Set outOutBox = outApplication.GetNamespace("MAPI").GetDefaultFolder(5)



Rem First InBox email

MsgBox outInBox.Items.Item(1).Recipients(1).Name, vbOKOnly, "Inbox Recipient"

MsgBox outInBox.Items.Item(1).Subject, vbOKOnly, "Inbox Subject"

MsgBox outInBox.Items.Item(1).Body, vbOKOnly, "Inbox Body"



Rem First SentMail email

MsgBox outOutBox.Items.Item(1).Recipients(1).Name, vbOKOnly, "SentMail Recipient"

MsgBox outOutBox.Items.Item(1).Subject, vbOKOnly, "SentMail Subject"

MsgBox outOutBox.Items.Item(1).Body, vbOKOnly, "SentMail Body"



None
Sending email using the MS Outlook object

Private Sub MrPostman(strSendTo As String, strSubject As String, strMessage As String)

  Dim outEmail As Outlook.Application

  Dim outNewMail As Outlook.MailItem

  Dim strTemp() As String



  Set outEmail = New Outlook.Application

  Set outNewMail = outEmail.CreateItem(olMailItem)



  With outNewMail



    strTemp = Split(strSendTo, ";")



    For intCounter = 0 To UBound(strTemp)

      .Recipients.Add Trim(strTemp(intCounter))

    Next intCounter



    .Subject = strSubject

    .Body = strMessage

    .Send

  End With



  Set outEmail = Nothing

  Set outNewMail = Nothing



End Sub


None
Calling procedures dynamically

Rem Use this code when you don't know the name of the procedure or when you want the user to select the procedure to execute

Private Sub Form_Load()

CallByName Form1, "Test", VbMethod

End Sub

Public Function Test()

  MsgBox "It Works"

End Function


None
Copy/Move files from one location to another

FileCopy "C:SourceFile.txt", "C:DestinationFile.txt"

Rem To move the file (delete the original)

Kill "C:SourceFile.txt"


None
Retained is an invalid key error

You will get this error when you attempt to open a project designed in VB6+ with VB5-.
The solution is to open the project file (*.vbp) with a text editor like notepad
and delete the line that begins with RETAINED=. This will solve the error.

None
What does referencing a control mean?
What is the difference between early and late binding?

When you create a reference to a control, you are indicating that there is a file that exists
that you would like to use. Early-binding indicates this reference at design-time of the application
rather than an runtime (late binding). Early binding is much faster than late binding. Late binding
is used when an application must determine at runtime. Although this process is slower than
late binding, it may be faster after consideration. For example, let's say that you are importing
data from one source to another. You are uncertain at design time wheter the user will want to
import from Excel to Access, Outlook to Excel, Outlook to Access, Excel to Outlook, Access to Outlook,
or Access to Excel. Instead of referencing all three objects at design time(early binding),it may
be more practical to refernce them once the user has mad a decision (late binding).

None

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 127 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.