Visual Basic 911 - FAQ (general)
Do you know any good web sites about VB
programming?
There is a lot of good VB sites. My personal favorites are:
http://www.netfokus.dk/vbadmincode
http://www.zarr.com/vb
http://www.mvps.org/vbnet
http://www.vb-helper.com/howtoadv.htm
http://www.jelsoft.com/vbw/tips/index.html
http://www.earthlink.net/~butlerbob/vb/index.htm
http://www.vbtt.com
What are the best VB books? I'd like to learn VB by
myself.
Check the list of my favorite books. Most
of these books are available from www.amazon.com (with discounts) and
from www.bookpool.com (with even bigger discounts).
You also might want to check Personal Bookshelf website at http://www.mcp.com/personal
they have a lot of VB books on-line. Don't forget Microsoft Knowledge Base (http://support.microsoft.com/support )
and MSDN library ( http://premium.microsoft.com/msdn/library)
How to convert BMP to GIF or JPG in Visual Basic?
Visual Basic Image control lets you save GIF/JPG image to BMP format, but not vice versa. I've heard there is a third-party
VB control doing opposite conversion. You might want to check www.activex.com
When I tried to open the project written on VB6 with Visual Basics 5, I
got an error that says: "Retained is an invalid key. The file C:\Windows\Desktop\Project1.vbp can't be
loaded".
VB6 has additional keys in .vbp file that previous versions of VB don't
recognize. You can open .vbp file in any text editor (notepad, WordPad) and simply remove
the whole string containing "Retained" key. After that it should work with VB4/5
I have an access database with 5 tables in
it. These tables are vendors for parts. I want to build a program that will show the prices of the
part number I type in my text box. I can not figure out how to search all the tables PartNumber
fields as one function. These are very large tables so I had planned on using the seek function but
haven't figured out how yet.
Index PartNumber field in each table (to make search faster) and use SQL
statement instead of Seek method:
set rst=dbs.openrecordset("SELECT price FROM table1, table2, ... WHERE
partnumber=" & lPartNumber)
if not rst.eof then
msgbox rst!price 'or show it in textbox: tex1.text=rst!price
else
msgbox "no price found for specified part"
end if
rst.close
How can I edit the registry within VB?
How do I change keys, how do I add them?
You might want to check VB online magazine at http://www.vbonline.com/vb-mag/qaweb/REGINI/REGISTRY.HTM.
They have a nice project defining all functions you need to access and edit Registry
How do I create a new System DSN for
SQL from a program written in VB?
check following link: http://support.microsoft.com/support/kb/articles/q184/6/08.asp
Do you know of a good book or guide to help me program in VB, so I may create
modules for Access?
Looks like you need book on VBA, not VB. This one is good to start from:
Beginning Access 97 VBA Programming, by Robert Smith, David Sussman
This one is a little more advanced:
Access 97 Developer's Handbook, by Paul Litwin
Also you might want to start with Access help - there is a lot on using
VBA.
How do I take a single line of text from a file and store it in a
variable? How do I save variables to files?
Dim strTemp As String
Dim strFile As String
Private Sub WriteVariable() 'writes variable to file
strFile = "c:\temp\test.txt" 'your file with full path
'if file doesn't exist - program will create it
strTemp = InputBox("Enter anything") 'load some string to variable
Open strFile For Output As 1 'open text file for writing
Print #1, strTemp 'writing variable to file
Close #1
End Sub
Private Sub GetLine() 'read first string from file to variable
strFile = "c:\temp\test.txt" 'your file with full path
file must exist before you read from it
Open strFile For Input As 1 'open text file for reading
Line Input #1, strTemp 'read first string to variable
Close #1
MsgBox strTemp 'show what we've got
End Sub
Private Sub Command1_Click()
WriteVariable
End Sub
Private Sub Command2_Click()
GetLine
End Sub
How do I validate the date entered in
textbox?
Try this sample. Open new project, add command button (Command1) and textbox (Text1). Enter the following code:
Private Sub Command1_Click()
If IsDate(Text1.Text) Then
MsgBox CDate(Text1.Text)
Else
MsgBox "Invalid date entered"
End If
End Sub
How can I get an event to be triggered when file is added to or deleted from a subdirectory?
Check this sample project
I'm trying to use double-click on a MSFlexGrid using a
selected row. The selected row has Product Number(col1), Product Description (col2) and Product Price (col3). I would like to use double-click to
transfer (copy) that information into a control array with the names NumberArray, Description Array, and Price array.
Try this code (it uses string arrays, since I didn't know what type of control you're using in your control arrays)
Dim aNumber() As String
Dim aDescription() As String
Dim aPrice() As String
Private Sub Form_Load()
ReDim aNumber(0)
ReDim aDescription(0)
ReDim aPrice(0)
End Sub
Private Sub MSFlexGrid1_DblClick()
MSFlexGrid1.Col = 1
aNumber(UBound(aNumber())) = MSFlexGrid1.Text
ReDim Preserve aNumber(UBound(aNumber()) + 1)
MSFlexGrid1.Col = 2
aDescription(UBound(aDescription())) = MSFlexGrid1.Text
ReDim Preserve aDescription(UBound(aDescription()) + 1)
MSFlexGrid1.Col = 3
aPrice(UBound(aPrice())) = MSFlexGrid1.Text
ReDim Preserve aPrice(UBound(aPrice()) + 1)
End Sub
I've used you "Using
Outlook Address Book" project successfully few times. However, it
returns
Automation error if Outlook 97 is installed instead of Outlook 98. Is there a way that I can make my VBA compatible to Outlook 97, if not
how do I detect the
version of the Outlook and prompt the user to upgrade to Outlook 98?
You can't run this project with Outlook 97, since it doesn't allow
access to Address Book.
To check version you can try following code:
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
MsgBox olApp.Version
olApp.Quit
Set olApp = Nothing
How to run a Project from the
SystemTray?
Check the following URL: http://www.vb-helper.com/Howto/tray2.zip
I have a batch file which I would now like to convert to VB 5.0. What are
the relevant commands? I need to use like MD, DELTREE, etc.
Analog for MD is MkDir
There is no analog for DELTREE. You have to delete all files in
directory first, and after that delete directory.
Analog for DEL is Kill
Analog for RD is RmDir
Check VB help for exact syntax
How to start remote programs (i.e.: notepad, calculator) using
version 5.0 learning or 6.0 enterprise editions of VB?
Use Shell function. It is available in all editions and versions of VB. For example:
Shell c:\winnt\notepad.exe, vbNormalFocus
I developed a database application in
VB 5. I am using MS Access as backend. In front-end I used List boxes, Buttons, MS tab
Controls, etc.. Is it possible to make this application web enabled?
It is possible up to the certain point. If you are going to use IE only - you can convert your project into ActiveX document, that will behave
almost exactly like your application did. If you care about Netscape users - you need to re-write interface in HTML (using forms). If you want to continue to use Access as backend - you have to use
one of the Microsoft web servers
(IIS or PWS). In that case you can connect to database using ADO/ASP
Is there an easy way to resize all the objects when resizing the
form?
There is no easy way. You have to catch new form' size and recalculate all the components' sizes. For example, following code keeps textbox as big
as possible and command button in the middle of the form:
Private Sub Form_Resize()
On Error Resume Next
Text1.Width = Me.Width - 60
Text1.Height = Me.Height - 960
Command1.Top = Me.Height - Command1.Height - 430
Command1.Left = (Me.Width - Command1.Width) / 2
End Sub
How do I make a self extractor that automatically
gets all the dll's it need and puts them all into one file?
It takes two steps to create self-extracting setup executable.
1)Use VB Setup wizard (or Package and Deployment wizard) to create setup package - bunch of files including your
application executable with dependencies (dlls, ocxs, etc). If you are familiar with Installshield, you can use it for that purpose as well.
2)Compile all the files in one executable. The best tool for it is Installshiled PackageForTheWeb. But there is a lot of utilities
you can choose from: ZIP-selfextractor, GSX-selfextractor, Instyler,
etc.
How can I find if table exists in a database?
1.set On Error Resume Next
2.try to open the table you're looking for
3.catch the error (if table exists - err.Number=0)
Here is the code for Access database:
On Error Resume Next
Set dbs = CreateObject("ADODB.Connection")
dbs.open "provider=Microsoft.Jet.OLEDB.4.0;data
source=c:\yourpath\youfile.mdb"
Set rst = dbs.execute("yourtable")
If Err.Number <> 0 Then MsgBox "Table not found" Else MsgBox "OK"
How can I make a Help-File? Can I make this
file with VB 4?
You can't make help file with VB only.
Fortunately Microsoft provide programmers with Help Workshop. It is located on VB CD under \Tools directory. It's pretty basic tool. If you need something more powerful - check Robohelp.
What are the best VB Magazines other than Visual Basic
Programmer's Journal ( VBPJ) and Access/VB Advisor?
You should definitely check Software Development magazine. It's not Visual Basic-specific, but if you do some serious programming - you are going to like it. Best of all - subscription is free for qualified subscribers.
http://www.sdmagazine.com
VB Online Magazine is very good. Though there is no printed version. http://www.vbonline.com/vb-mag
That's pretty much it. I have to mention Microsoft MSDN library that has more VB information than all VB magazines altogether. It's also free (registration required).
http://msdn.microsoft.com
I could not create a flexgrid. Where is the control button?
Flexgrid is nod intrinsic (standard) Visual Basic control. In order to use it in your project you need to click on "Project" menu
and select "Components" submenu. Dialog box with list of available controls will appear. Find "Microsoft Flexgrid Control" and check its checkbox. Click on "OK" button to close "Components" dialog box.
Flexgrid icon should appear on "Toolbox" window.
Where can I find examples on writing
classes in VB?
Go to Visual Basic Books Online, select "Using Visual Basic/Part 2:..../Programming with Objects"
There is a lot of info on classes.
How do I make an RTF box un-editable?
I think you are talking about Microsoft Rich TextBox Control. Set its property "Locked" to "True".
Under VB6, when I press the F1 key to get help, it tells me "The MSDN collection does not
exist. Please reinstall MSDN"... What does it mean?
You didn't install MSDN during VB installation.
VB 6 has its help located in NSDN. Here is what you need to do:
Run VB setup, click Add/Remove button and install MSDN.
You can have it running from CD (saves you about 600 MB hard-drive
space) or install contents on your hard-drive (runs faster).
I am using DBGRID for entering data
in unbound mode. How do I restrict user to a certain number of rows?
You need to keep truck of number of rows and compare it with row limit
you set.
Something like this
Private Sub DBGrid1_UnboundAddData(ByVal RowBuf As MSDBGrid.RowBuffer, NewRowBookmark As Variant)
iRows = iRows + 1
If iRows > iLimit Then DBGrid1.AllowAddNew = False
End Sub
Private Sub DBGrid1_UnboundDeleteRow(Bookmark As Variant)
iRows = iRows - 1
If iRows < iLimit Then DBGrid1.AllowAddNew = True
End Sub
Where iRows - variable that keeps current number of rows
and iLimit - variable that keeps maximum number of rows allowed
I'm developing reports in VB6 using Data Report and have problems changing the
printer setup on the user's machine (he doesn't have permissions to change the page setup properties of the
printer) Since he can't change the
default from Portrait to Landscape the report won't run - because of "paper size"
error. I want to change the properties of the printer just for the duration of the program
(like Word does) but can't. I even tried changing it via API - no access to
printer!
If user can't change printer properties (doesn't have access to it), than APIs from system, using user credentials (restricted access to printer properties) can't change it either.
How do I save the index of a listbox as a file
?
I'm not clear what index you are talking about. Lets review all possibilities:
1)Index property of listbox controls array:
Open "c:\temp.txt" For Output As 1
Print #1, Listbox1(n).Index
Close #1
2)ListIndex property of listbox (index of currently selected item):
Open "c:\temp.txt" For Output As 1
Print #1, Listbox1.ListIndex
Close #1
3)Contents of the listbox (all the elements in listbox):
Open "c:\temp.txt" For Output As 1
For i=0 To Listbox1.ListCount-1
Print #1, Listbox1.List(i)
Next
Close #1
Is there a way to run VB5 programs without having MSVBVM50.DLL?
Could the source be compiled with only the needed information, and not just have
that extra 1.4Mb baggage?
No. This dll is vital for any VB program, since it contains all the definitions of VB functions, types, variables, etc. Without it OS simply will not recognize a single byte of VB code.