Results 1 to 5 of 5

Thread: Adding rows to a dbgrid

  1. #1
    New Member
    Join Date
    Jun 99
    Posts
    7

    Post

    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)

    RXNUMBER.Visible = True
    TOY.Visible = True
    ROOM.Visible = True
    PNAME.Visible = True
    DIRECT.Visible = True
    DRUGNAME.Visible = True
    STRENGTH.Visible = True
    AMOUNT.Visible = True
    DOCTOR.Visible = True
    OTHERDIR.Visible = True

    RXNUMBER.Caption = "Rx Number"
    TOY.Caption = "Date"
    ROOM.Caption = "Room #"
    PNAME.Caption = "Patient Name"
    DIRECT.Caption = "Directions"
    DRUGNAME.Caption = "Drug Name"
    STRENGTH.Caption = "Strength"
    AMOUNT.Caption = "Amount"
    DOCTOR.Caption = "Doctor"
    OTHERDIR.Caption = "Other Directions"

    RXNUMBER.Width = 900
    TOY.Width = 800
    ROOM.Width = 800
    DIRECT.Width = 2000
    DRUGNAME.Width = 1100
    STRENGTH.Width = 800
    AMOUNT.Width = 800
    DOCTOR.Width = 1000
    OTHERDIR.Width = 1900

    End Function

    This funtion works to set up the columns of the grid, but how do you insert rows by code?
    ANY HELP WOULD BE GREATLY APPRECIATED.

  2. #2
    Junior Member
    Join Date
    Jan 99
    Location
    Seattle WA USA
    Posts
    21

    Post

    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.

    ------------------

  3. #3
    New Member
    Join Date
    Jun 99
    Location
    NJ
    Posts
    1

    Post

    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

    End If

  4. #4
    Lively Member
    Join Date
    Jan 99
    Location
    Israel
    Posts
    79

    Post

    i dont know if it helps, but if you use the data object, try this code and check what it dode:

    data1.recordset.addnew


  5. #5
    New Member
    Join Date
    Jun 99
    Posts
    7

    Post

    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.

Posting Permissions

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