Solved

Visual Basic

Posted on 2008-10-22
4
254 Views
Last Modified: 2010-05-18
how can i parse a junk to data to print in vb? i mean if my database has 100 data, how can i print the data from 50 to 70?

thanks
0
Comment
Question by:tancy1
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:mdougan
ID: 22787851
What version of VB are you using?  What part are you having trouble with, the retrieving of data from the database, or the printing part, or both?

Can you display any code that you have already?

What kind of database are you using?
0
 

Author Comment

by:tancy1
ID: 22792577
hi ... using vb6 and access 2000. didnt do any coding yet.. i was think to put up a form and ask the user to enter the data that they want to print.
eg print a receipt, from bill no 5100 to 5250.

thank you.
0
 
LVL 18

Accepted Solution

by:
mdougan earned 50 total points
ID: 22795851
OK, well, here is an outline of what you want to do.  To use this code, you would need to go to your Project References and select the library for ADO objects.  Then, I coded two references to some textboxes called txtBillStart   and txtBillEnd, put those on your form, maybe add a command button, and, in the click event for the command button, just call

PrintData

Copy the variables at the top of this code into the General Declarations section of your form, and copy the other two procedures into the code area of your form.

Look through the code, and in the Connect routine, you'll need to change the name and location of the database that you want to open.  This code is looking into the same directory the program is running in for a database called data.mdb.

In the PrintData routine, I wrote a select statement to retrieve data from a database table.  You'd have to change the SQL so that it is selecting from a real table in your database, and using real field names.

Then, you could try testing.


Dim CN As ADODB.Connection

Dim CMD As ADODB.Command

Dim RS As ADODB.Recordset
 

Private Function Connect() As Boolean
 

    Set CN = New ADODB.Connection

    With CN

        .CursorLocation = adUseClient

        

        ' this is going to open an access database in the program's directory named data.mdb... change it to the name and location of your database

        .ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & IIf(Right(App.Path, 1) = "\", App.Path, App.Path & "\") & "DATA.mdb"

        

        .Open

    End With

    

    If CN.State <> adStateOpen Then

        MsgBox "Uable to open the database.  Uable to continue.", vbExclamation, "Login"

        Connect = False

    Else

        Connect = True

    End If
 

End Function
 
 

Private Sub PrintData()

Dim sSQL As String

Dim i As Long

Dim J As Long
 

  

    On Error Resume Next

    

    If NOT Connect() Then Exit Sub

        

    sSQL = "SELECT bill_no, bill_date, bill_amt from billing WHERE bill_no BETWEEN " & txtBillStart.Text & " AND " & txtBillEnd.Text & " ORDER BY bill_no ASC"

    

    Set CMD = New ADODB.Command

    With CMD

        .CommandText = sSQL

        .CommandType = adCmdText

        .ActiveConnection = CN

    End With

    

    Set RS = CMD.Execute

    If CN.Errors.Count > 0 Then

        MsgBox CN.Errors(0).Description

    End If

    

    If RS.State <> adStateOpen Then GoTo EXITRTN

    

    If RS.EOF Or RS.BOF Then GoTo EXITRTN

    

    RS.MoveFirst

    

    Printer.BeginDoc

    

    While Not RS.EOF

        Printer.Print RS("bill_no").Value & chr(9) & RS("bill_date").Value & chr(9) & RS("bill_amt").Value        

        RS.MoveNext

    Wend

    

    Printer.EndDoc

            

EXITRTN:

    Set CMD = Nothing

    Set RS = Nothing

    

End Sub

Open in new window

0
 

Author Comment

by:tancy1
ID: 22818442
thanks for your input. but if i want to sent the data to datareport, how should i do? thanks again
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb6 programatically bring window to front 8 65
VB6 code to programmatically convert pdf to excel 21 70
VBA color chart bars 12 78
VB6 Compile Compatibility Issue 4 90
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now