Results 1 to 32 of 32

Thread: Print database to picbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Red face Print database to picbox

    I have a few questions on getting data and displaying it.

    Question 1
    I have customer information saved to a file, Customer.dat, how can I display the contents of the file in a picture box??

    Question 2
    also from the same customer database, how can I run reports like top 5 (€ value) customers
    How can I divide up the data in the database to run certain queries?

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Print database to picbox

    what database is this?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Print database to picbox

    Why are you looking to display the contents of a database in a picture box?

    What kind of data would you be displaying?

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    it's a datafile, it's not an actual database like access.

    the reason for the picture box is that it can only be viewed and not edited by people who do not have permission to. Info like customer name, address etc would be displayed

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Print database to picbox

    You could use textboxes and set their Locked property to True, or If you really want to use pic boxes then you might want to use the DrawText API, simple example.

    Code:
    Option Explicit
    
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
     
    Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDC As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
    Private Const DT_WORDBREAK = &H10
    Code:
    Private Sub Command1_Click()
    
        Dim rct As RECT
        
        Picture1.ScaleMode = vbPixels
        
        With rct
            .Left = 0
            .Right = Picture1.ScaleWidth
            .Top = 0
            .Bottom = Picture1.ScaleHeight
        End With
         
        DrawText Picture1.hDC, "Text to Draw to my Picture Box", -1, rct, DT_WORDBREAK
    
    End Sub

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Print database to picbox

    Quote Originally Posted by klemster
    it's a datafile, it's not an actual database like access.

    the reason for the picture box is that it can only be viewed and not edited by people who do not have permission to. Info like customer name, address etc would be displayed
    You are writing a computer program. This program access a file. It takes the information in this file and displays it.

    Without your program, the data wouldn't be displayed.

    If you don't want people to edit the data, then don't write anything into your program that permits editing. How it is displayed is beyond definition of irrelevant. If your program does nothing but display, then the user can do nothing but view.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    Quote Originally Posted by Edgemeal
    You could use textboxes and set their Locked property to True, or If you really want to use pic boxes then you might want to use the DrawText API, simple example.

    Code:
    Option Explicit
    
    Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End Type
     
    Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDC As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
    Private Const DT_WORDBREAK = &H10
    Code:
    Private Sub Command1_Click()
    
        Dim rct As RECT
        
        Picture1.ScaleMode = vbPixels
        
        With rct
            .Left = 0
            .Right = Picture1.ScaleWidth
            .Top = 0
            .Bottom = Picture1.ScaleHeight
        End With
         
        DrawText Picture1.hDC, "Text to Draw to my Picture Box", -1, rct, DT_WORDBREAK
    
    End Sub
    where can I put a file name/path so it opens the file?

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Print database to picbox

    Quote Originally Posted by klemster
    where can I put a file name/path so it opens the file?
    You may want to search or post that as a new question, describe what sort of data is in the file, like is it one string item per line, strings separated by commas, etc, etc?

    btw, Drawtext API is nice cause you can position the text and it also has other options like word break so you can do paragraphs, but if you are just putting a name in a pic box you could simply use .Print

    Picture1.Cls
    Picture1.Print "My Name"
    Last edited by Edgemeal; Mar 24th, 2008 at 09:00 AM.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    i can't use .print to print a data file.
    What's the easiest way of letting people view all the information in a file?

  10. #10
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Print database to picbox

    How about simply displaying it in a textbox?

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    how would I go about that?

  12. #12
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Print database to picbox

    vb Code:
    1. Sub openfile()
    2. Dim f As Integer, astring() As String, i As Integer
    3. f = FreeFile
    4. Open "somepath\filename.dat" For Input As f
    5.     astring = Split(Input(LOF(f), #f), vbNewLine)
    6.     Close f
    7. For i = 0 To UBound(astring)
    8.     Picture1.CurrentX = 30   ' put a margin so it is not printed hard against the edge
    9.     Picture1.Print astring(i)
    10. Next
    11. End Sub
    this is a simple code to open a file, read the entire content into an array, then print the array into a picturebox, with a small margin on the left side
    to put the file into a textbox is easy too
    vb Code:
    1. Sub openfile()
    2. Dim f As Integer
    3. f = FreeFile
    4. Open "somepath\filename.dat" For Input As f
    5.     text1.Text = Input(LOF(f), #f)
    6.     Close f
    7. End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Print database to picbox

    Make sure the textbox is set to multiline and you have added scrollbars.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    I am getting a run-time error File Already Open, on certain files if I edit the file before viewing the file in the picture box.

  15. #15
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Print database to picbox

    Is your program the one that has it open, or is some other program using them?

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    my programme, it's all the one programme!

  17. #17
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Print database to picbox

    Ok, well that actually makes it a bit easier.

    There are a lot of code suggestions on this thread. Which one are you using to open these files?

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    This is the code that Westconn1 posted earlier for the pic box:
    Code:
      Private Sub Openfile()
       
          Dim f As Integer, astring() As String, i As Integer
       
          f = FreeFile
       
          Open App.Path & "\Driver.dat" For Input As f  
       
              astring = Split(Input(LOF(f), #f), vbNewLine)
       
              Close f
       
          For i = 0 To UBound(astring)
                
                picShow.Cls
                picShow.CurrentX = 100   'a margin so it is not printed hard against the edge
                  
                picShow.Print astring(i)
          Next
          End Sub
    This is the code that I am using to add/save/edit information
    Code:
    Option Explicit
    Private Sub cmdAdd_Click()
        'Add a new record
        
        lblAction.Caption = "Action: Add"
        With cboDriver
            .ListIndex = -1     'Deselect current entry
            .Enabled = False
        End With
      
    End Sub
    
    
    Private Sub cmdEdit_click()
    
        'Edit the current employee information
        Dim strMsg  As String
        
        If cboDriver.ListIndex <> -1 Then     'Record selected
            lblAction.Caption = "Action: Edit"
            cboDriver.Enabled = False
            DisableButtons
            UnlockTheControls
        Else
            strMsg = "Select record to edit."
            MsgBox strMsg, vbInformation, "Delivery List"
        End If
        
    End Sub
        
    
    Private Sub cmdSave_Click()
    
        'save the contents of the controls in file
        'after an Edit or Add
        
        Dim strMsg As String
        
        If txtLastName.Text <> "" And txtFirstName.Text <> "" And txtStreet.Text <> "" And txtCity.Text <> "" _
        And txtCounty.Text <> "" And txtPhone.Text <> "" And txtArea.Text <> "" Then
            SaveRecord
            lblAction.Caption = "Action: Record Saved"
            cboDriver.Enabled = True
            EnableButtons
            LockTheControls
        Else
            strMsg = "Please complete all fields"
            MsgBox strMsg, vbInformation, "Delivery List"
            txtLastName.SetFocus
        End If
           
    End Sub
    
    Private Sub Form_Load()
        'initialise file and controls
        
        ReadFileIntoList
          
    End Sub
    
    Private Sub mnuFileExit_click()
        'terminate the project
        
        Close #1
        Unload Me
        End
        
    End Sub
    
    Private Sub AddToList(intRecordNum As Integer)
        'Add a customer to the list and to ItemData
        Dim strName As String
        
        strName = Trim(mudtDriver.strLastName) & ", " & mudtDriver.strFirstName
        With cboDriver
            .AddItem strName
            .ItemData(.NewIndex) = intRecordNum
        End With
        
    End Sub
    
    
    
    Private Sub DisplayData()
        'Display the data for the selected employees
        
        With mudtDriver
            txtLastName.Text = .strLastName
            txtFirstName.Text = .strFirstName
            txtStreet.Text = .StrStreet
            txtCity.Text = .strCity
            txtCounty.Text = .strCounty
            txtPhone.Text = .strPhone
            txtArea.Text = .strArea
            
            
        End With
        lblAction.Caption = "Action: Display"
        LockTheControls
        EnableButtons
        
    End Sub
    
    Private Sub ReadFileIntoList()
        'read the file and store in the sorted combo box
        'store the relative record number into ItemData
        
        Dim intRecordNum As Integer
        Dim intResponse As Integer
        Dim strMsg As String
        
        On Error GoTo HandleErrors
        Open App.Path & "\Driver.dat" For Random As #1 Len = Len(mudtDriver)
        If LOF(1) / Len(mudtDriver) > 0 Then 'if file not empty
            For intRecordNum = 1 To LOF(1) / Len(mudtDriver)
                Get #1, intRecordNum, mudtDriver
                If mudtDriver.strDeleteCode = "A" Then 'Active record
                    AddToList (intRecordNum)
                End If
                
            Next intRecordNum
        Else
            strMsg = "File does not exist. Create new file?"
            intResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Driver File")
            If intResponse = vbNo Then
                mnuFileExit_click
            End If
        End If
        
    ReadFile_Exit:
        Exit Sub
        
    Private Sub SaveRecord()
        'save the current record for an Edit or Add
        
            Dim intRecordNum As Integer
            Dim strName As String
            
            If lblAction.Caption = "Action: Edit" Then
                intRecordNum = cboDriver.ItemData(cboDriver.ListIndex)
                With cboDriver    'remove name from list
                    cboDriver.RemoveItem cboDriver.ListIndex
                End With
            Else                    'Action: Add
                intRecordNum = LOF(1) / Len(mudtDriver) + 1 'find next record number
            End If
            SetupRecord
            WriteRecord intRecordNum
            With cboDriver          'Add name to list
                strName = Trim(mudtDriver.strLastName) & ", " & mudtDriver.strFirstName
                .AddItem strName
                .ItemData(.NewIndex) = intRecordNum
            End With
            EnableButtons
            LockTheControls
            lblAction.Caption = "Action: Record Saved"
            cboDriver.Enabled = True
    End Sub
    
    Private Sub SetupRecord()
    
        'set up record from screen controls
        
        With mudtDriver
            .strLastName = Trim(txtLastName.Text)
            .strFirstName = Trim(txtFirstName.Text)
            .StrStreet = Trim(txtStreet.Text)
            .strCity = Trim(txtCity.Text)
            .strPhone = Trim(txtPhone.Text)
            .strCounty = Trim(txtCounty.Text)
            .strArea = Trim(txtArea.Text)
            
            .strDeleteCode = "A"
        End With
    End Sub
    
    Private Sub WriteRecord(intRecordNum As Integer)
        'write the current record in the file
        'used for Edit, Add, and Delete
        
        Put #1, intRecordNum, mudtDriver
    End Sub
    Thanks!!

  19. #19
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Print database to picbox

    ReadFileIntoList I'm guessing is where you are getting the error and I don't see a Close #1 there, so the next time through, it is still open when you issue your Open statement.

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    I am getting another run time error now, Input past end of file, on this
    Code:
    astring = Split(Input(LOF(f), #f), vbNewLine)
    I don't have msdn help installed so don't know what that means!

  21. #21
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Print database to picbox

    it is to do with the way you save your data, it looks as if the file is longer than the data, so it gives an error
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    how can I come around that?

  23. #23
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Print database to picbox

    probably have to open the file for random, as that is the way you are writing the records to the file, then read all the records into an array to print the array to the picturebox,
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    would you be able to post some code because I don't really understand

  25. #25
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Print database to picbox

    you could try like this, but i have not tested it
    vb Code:
    1. Dim f As Integer, astring() as string, i as integer
    2. redim astring(500) 'allow for 500 lines in file
    3. f = FreeFile
    4. i = 0
    5. Open "somepath\filename.dat" For Input As f
    6. do while not eof(f)   ' loop till end of file, not length of file
    7.    Line Input #f, astring(i)
    8.    i = i +1
    9. loop
    10. Close f
    11. redim preserve astring(i - 1)    ' reset array to actual number of lines
    then use the same code i posted before to print to picbox or you can just change the line input to print directly into the picbox instead of using array, the you dont need to declare the array at all or worry about how big to make it
    Line Input #f , somestring
    picture1.print somestring
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  26. #26

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    cool that seems to be working, thanks so much! One thing left, the information is being displayed in one line, how can you make it multi line?

  27. #27
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Print database to picbox

    is this in a textbox or picbox?
    if textbox set multiline property to true
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  28. #28

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    no its a pic box!

  29. #29
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Print database to picbox

    post your current code for that bit
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  30. #30

    Thread Starter
    Junior Member
    Join Date
    Mar 2008
    Location
    Cork, Ireland
    Posts
    24

    Re: Print database to picbox

    it's the code from above that you gave me! Will I post it anyway?

  31. #31
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Print database to picbox

    no need, i think the data in the file is a single line, so line input only reads one line then puts that n the picturebox, looks like you will need to read each record in turn and print it to the picturebox, i don't normally work with files opened for random
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  32. #32
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Print database to picbox

    i guess ou need to do like this, i just edited some of your other code for you to try
    vb Code:
    1. Open App.Path & "\Driver.dat" For Random As #1 Len = Len(mudtDriver)
    2.     If LOF(1) / Len(mudtDriver) > 0 Then 'if file not empty
    3.         For intRecordNum = 1 To LOF(1) / Len(mudtDriver)
    4.             Get #1, intRecordNum, mudtDriver
    5.             If mudtDriver.strDeleteCode = "A" Then 'Active record
    6.                 picture1.print mudtDriver.strfirstname & mudtDriver.strlastname ' and so on
    7.             End If
    8.            
    9.         Next intRecordNum
    10.     Else
    11.         strMsg = "File does not exist. Create new file?"
    12.         intResponse = MsgBox(strMsg, vbQuestion + vbYesNo, "Driver File")
    13.         If intResponse = vbNo Then
    14.             mnuFileExit_click
    15.         End If
    16.     End If
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width