DBGrid Scroll Memo Field

I need to auto-adjust the column size in my DBGrid if the text runs outside of the predefined column size .. or

have the row size increase to show all text entered (kindof like a wrap).
KathyBrowningAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jgvCommented:
Altering the size of a cell in the grid to suit the text would be a real pain in the a**. This example will pop up a text box that will display the contents of the cell (to make it easier to read/edit). If there are changes made within the text box then the cell will be updated. I figured that you wanted to use this in conjunction with your 'default value' column so the code includes both. If you have already set up the the test project for your other Q, then you just have to add a textbox, set the 'MultiLine' property to True and use this code.

Option Explicit

Dim ListValues(2) As String
Dim textbox_value_changed As Boolean

'//change these values to suit your column #'s
Const DefaultCol = 2 'column # for default value
Const MemoCol = 5 'column # for notes field

Private Sub Form_Load()
    ' Add choices to array
    ListValues(0) = "Primary"
    ListValues(1) = "Contractor"
    ListValues(2) = "Other"
   
    ' add array values to List1
    Dim x
    For x = 0 To 2
        List1.AddItem ListValues(x)
    Next
   
    ' Place a button in the default column and lock
    DBGrid1.Columns(DefaultCol).Button = True
    DBGrid1.Columns(DefaultCol).Locked = True
    DBGrid1.Columns(MemoCol).Button = True
   
    List1.Visible = False
    List1.BackColor = &HC0FFC0
   
    Text1.Visible = False
    Text1.BackColor = &HC0FFC0
End Sub

Private Sub DBGrid1_ButtonClick(ByVal ColIndex As Integer)
   
    ' Assign the Column object to Co because it will be used
    ' more than once.
    Dim Co As Column
    Set Co = DBGrid1.Columns(ColIndex)
   
If ColIndex = DefaultCol Then
   
    If List1.Visible = True Then
        List1.Visible = False
        Exit Sub
    End If
    ' Position and drop down List1 at the right edge of the
    ' current cell.
    With List1
        .Left = DBGrid1.Left + Co.Left
        .Top = DBGrid1.Top + DBGrid1.RowTop(DBGrid1.Row) + DBGrid1.RowHeight
        .Width = Co.Width
        .Visible = True
        .ZOrder 0
        .SetFocus
    End With
   
ElseIf ColIndex = MemoCol Then
    ' update column if Textbox is visible,
    ' then hide it
    If Text1.Visible = True Then
        Textbox_To_Column
        Exit Sub
    End If
   
    With Text1
        .Text = Trim(Co.Value)
        .Left = DBGrid1.Left + Co.Left
        .Top = DBGrid1.Top + DBGrid1.RowTop(DBGrid1.Row) + DBGrid1.RowHeight
        .Width = Co.Width * 2
        .Visible = True
        .ZOrder 0
        .SetFocus
    End With

End If
End Sub

Private Sub DBGrid1_Scroll(Cancel As Integer)
    ' Hide the list if we scroll.
    List1.Visible = False
    ' If Textbox is visible, check for changes then hide it
    If Text1.Visible = True Then
        Textbox_To_Column
    End If
End Sub

Private Sub DBGrid1_OnAddNew()

Dim DefValue As String

Select Case Me.Name '//get form name to determine default
    Case "frmMain"
        DefValue = ListValues(0)
    Case "frmContractor"
        DefValue = ListValues(1)
    Case Else
        '//unspecified form...do not insert a default
        Exit Sub
End Select

DBGrid1.Columns(DefaultCol).Value = DefValue

End Sub

Private Sub List1_Click()
    ' When an item is selected in List1, copy it to
    ' the column, then make List1 invisible.
    DBGrid1.Columns(DefaultCol).Value = List1.List(List1.ListIndex)
    List1.Visible = False
End Sub

Private Sub List1_LostFocus()
    ' Hide the list if it loses focus.
    List1.Visible = False
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
'//set a flag to indicate that the contents of the
'//text box have changed.
textbox_value_changed = True
End Sub

Private Sub Text1_LostFocus()
    ' update column, then hide textbox
    Textbox_To_Column
End Sub

Private Sub Textbox_To_Column()

'//if there were changes in the text box then copy the
'//contents to the Memo column.
If textbox_value_changed = True Then
    DBGrid1.Columns(MemoCol).Value = Trim(Text1.Text)
    textbox_value_changed = False
End If
   
Text1.Visible = False

End Sub
0
jgvCommented:
Can I assume that this is what you were looking for? As usual, if you have any questions about this code please let me know.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.