I am trying to make a database with the dbgrid control. So far, I have added 10 columns and set the columns's names and captions. But how do you add rows to the grid by code? The code I have so far goes as follows:
The following is inserted into the general section of a module.
'toy stands for time of year(date screws things up)
Public RXNUMBER, TOY, ROOM, PNAME, DIRECT, DRUGNAME, STRENGTH, _
AMOUNT, DOCTOR, OTHERDIR As Column
The following is a funtion that will set up the columns and rows of the dbgrid.
Function SetUpData()
'add columns
Dim x As Integer
x = 1
Do
Form1.DBGrid1.Columns.Add 0
x = x + 1
Loop Until x = 10
Set RXNUMBER = Form1.DBGrid1.Columns(1)
Set TOY = Form1.DBGrid1.Columns(2)
Set ROOM = Form1.DBGrid1.Columns(3)
Set PNAME = Form1.DBGrid1.Columns(4)
Set DIRECT = Form1.DBGrid1.Columns(5)
Set DRUGNAME = Form1.DBGrid1.Columns(6)
Set STRENGTH = Form1.DBGrid1.Columns(7)
Set AMOUNT = Form1.DBGrid1.Columns(8)
Set DOCTOR = Form1.DBGrid1.Columns(9)
Set OTHERDIR = Form1.DBGrid1.Columns(10)
If you bind to a table, the grid will set itself to the number of records in the table. You can set the VisibleRows property whether the DBgrid is bound to a table or not.
Try this code:
private Sub cmdAddRow_Click()
dim ncount as integer
ncount = MSFlexGrid1.Rows
'*'to add rows if GRID filled out , but additional row is needed
'*'1. Check, if now free space for data still available
'*'2. Add One row:
If MSFlexGrid1.TextMatrix(ncount - 1, 0) <> "" Then
MSFlexGrid1.Rows = ncount + 1
ncount = ncount + 1
MSFlexGrid1.Row = MSFlexGrid1.Rows - 1
MSFlexGrid1.Col = 1
MSFlexGrid1.CellBackColor = &H8000000F
MSFlexGrid1.GridColor = vbBlack
Thanks everyone! The data1.recordset.addnew turned out to be the most effective way to do it. Thanks again, and if ya have the time, take a look at my other topic...I would really appreciate it.