DBGrid control=set maxlength.

Posted on 1998-10-01
Last Modified: 2013-12-25
Is there a way to set the maxlength of a field in a data grid contol?
Question by:RUSTY

Expert Comment

ID: 1496576
No there is no direct way to set maxlength of a field. but you can do it by writing some code in keypress event or beforeupdate event, which will check the length of text and if it is more than maximum length you can prompt to user or whatever you want.

Author Comment

ID: 1496577
I've been trying to write some code to do this, but cannot get it to read the len of the field in the data grid column.  Can you post a snippet of code that will do this.  Thanks.

Author Comment

ID: 1496578
Adjusted points to 20
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center


Expert Comment

ID: 1496579
  True DBGrid Pro 5.0 by APEX (the upgrade to DBGrid) allows you to do this very simply.  It has a DataWidth property that you can set on a Column object.  
Ex: TDBGrid1.Columns(1).DataWidth = 255
I have used it ALOT. It prevents users from keying in data that is too long to fit in the field which as I'm sure you're aware causes an error.  I know this is probably not what you want to hear but I thought you might like to know... True DBGrid really is worth buying...

Accepted Solution

TheAnswerMan earned 20 total points
ID: 1496580
I assume you dont have TrueGrid Pro5..
That IS a pretty badass Grid Control, Its got some cool unbound grid features too.

'Flop this Code in
Private Sub Grid1_KeyPress(KeyAscii As Integer)
 if len(Grid1) > 10 then Keyascii = 0
End Sub

'this will keep you from entering more than 10 characters per field

Expert Comment

ID: 1496581
you could try this too.. if you know what size for each column is

Private Sub Grid1_KeyPress(KeyAscii As Integer)
 Select Case Grid1.col
 case 0
  if len(Grid1) > 1 then Keyascii = 0
 case 1
  if len(Grid1) > 2 then Keyascii = 0
 case 2
  if len(Grid1) > 3 then Keyascii = 0
 end select
End Sub

'this will allow one character in the first
2 in the second..
and three in the third

Author Comment

ID: 1496582
Ah.  Perfect.  Danke.  Aloha.  Tanx.  "A+"

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question