• 0

[VB] Database Project Help


Question

3 answers to this question

Recommended Posts

  • 0

For starters, you may want to consider closing your db connections and properly destroying objects. You may also want to code up the query unload of the form to clean up any resources on close.

Perhaps create a constant to hold your DB path so you down have all those hard coded paths.

Also, you should get in the habit of doing error handling.

As for saving to a text file here is an idea:

Public Sub SaveToTextFile 

Dim FileNum as long
Dim FileName as string

 ? ?On Error Goto ERR_HANDLER

 ? ?FileName = "C:\File.txt"
 ? ?FileNum = FreeFile

 ? ?Set db = OpenDatabase("C:\Bands.mdb ")
 ? ?Set rs as db.OpenRecordset ("Select * From Bands")
 ? ?If Not rs.EOF Then
 ? ? ? ?Open FileName for OutPut as #FileNum
 ? ? ? ?do until rs.eof
 ? ? ? ? ? ?print #FileNum rs!Name
 ? ? ? ? ? ?rs.movenext
 ? ? ? ?loop
 ? ?end if
 ? ?
 ? close #FileNum
 ? 
 ? rs.close
 ? set rs = nothing
 ? 
 ? db.close
 ? set db = nothing

 ? Exit Sub

ERR_HANDLER:
 ? ? ? ?
 ? msgbox Err.number & " - " & Err.Description

End Sub
 ?

This is pretty close and will create a text file that contains all the band names from the database.

As for printing, look for these help topics for Printer.Print and Printer.EndDoc

Edited by ixsis
Link to comment
Share on other sites

  • 0

Okay, i understand most of that, now how would i go about using the Common dialog Save to do that? Also, how do i use to common dialog control to print? My teacher posted the printing thing, but i didnt get a chance to copy it down. As for the sloppy stuff, i am a beginner programmer, and this is a project i havent really put much time into. And much thanks for the help by the way! :D

Link to comment
Share on other sites

  • 0

Common Dialog Save

CommonDialog1.ShowOpen
Filename = CommonDialog1FileName

Common Dialog Print

CommonDialog1.ShowPrinter

Printer.Print "Test"
Printer.EndDoc

Using the Common Dialog to select a printer will set the Printer object to the selected printer.

Resource cleanup and error handling really is something you might want to keep in mind especially as a beginner. VB is a forgiving language but it is still good to develop a good coding practice. This will be VERY important if you move beyond VB.

Good Luck

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.