-
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?
-
Re: Print database to picbox
-
Re: Print database to picbox
Why are you looking to display the contents of a database in a picture box? :confused:
What kind of data would you be displaying?
-
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
-
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
-
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.
-
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?
-
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"
-
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?
-
Re: Print database to picbox
How about simply displaying it in a textbox?
-
Re: Print database to picbox
how would I go about that?
-
Re: Print database to picbox
vb Code:
Sub openfile()
Dim f As Integer, astring() As String, i As Integer
f = FreeFile
Open "somepath\filename.dat" For Input As f
astring = Split(Input(LOF(f), #f), vbNewLine)
Close f
For i = 0 To UBound(astring)
Picture1.CurrentX = 30 ' put a margin so it is not printed hard against the edge
Picture1.Print astring(i)
Next
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:
Sub openfile()
Dim f As Integer
f = FreeFile
Open "somepath\filename.dat" For Input As f
text1.Text = Input(LOF(f), #f)
Close f
End Sub
-
Re: Print database to picbox
Make sure the textbox is set to multiline and you have added scrollbars.
-
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.
-
Re: Print database to picbox
Is your program the one that has it open, or is some other program using them?
-
Re: Print database to picbox
my programme, it's all the one programme!
-
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?
-
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!!
-
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.
-
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!
-
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
-
Re: Print database to picbox
how can I come around that?
-
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,
-
Re: Print database to picbox
would you be able to post some code because I don't really understand
-
Re: Print database to picbox
you could try like this, but i have not tested it
vb Code:
Dim f As Integer, astring() as string, i as integer
redim astring(500) 'allow for 500 lines in file
f = FreeFile
i = 0
Open "somepath\filename.dat" For Input As f
do while not eof(f) ' loop till end of file, not length of file
Line Input #f, astring(i)
i = i +1
loop
Close f
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
-
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?
-
Re: Print database to picbox
is this in a textbox or picbox?
if textbox set multiline property to true
-
Re: Print database to picbox
-
Re: Print database to picbox
post your current code for that bit
-
Re: Print database to picbox
it's the code from above that you gave me! Will I post it anyway?
-
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
-
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:
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
picture1.print mudtDriver.strfirstname & mudtDriver.strlastname ' and so on
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