Getting column widths from MSysObjects using SQL

%3CLINGO-SUB%20id%3D%22lingo-sub-1867725%22%20slang%3D%22en-US%22%3EGetting%20column%20widths%20from%20MSysObjects%20using%20SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1867725%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20work%20with%20an%20application%20that%20allows%20me%20to%20run%20SQL%20on%20the%20database%20to%20which%20it%20is%20connected.%20I%20want%20to%20be%20able%20to%20get%26nbsp%3Bcolumn%20widths%20for%20tables%20like%20I%20can%20using%20the%20system%20table%20USER_TAB_COLUMNS%20in%20ORACLE.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%2C%20I%20know%20that%20I%20can%20do%20this%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESELECT%20lvprop%20FROM%20msysobjects%20WHERE%20flags%20%3D%200%20AND%20type%20%3D%201%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20the%20data%20returned%20I%20can%20see%20(in%20plain%20text)%20'ColumnWidth'%20but%20the%20details%20are%20encoded%20in%20a%20manner%20I%20do%20not%20recognise%20and%20so%20cannot%20decode.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECould%20somebody%20please%20tell%20how%20I%20can%20mine%20column%20width%20data%20from%26nbsp%3Bmsysobjects.lvprop%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20ultimate%20goal%20is%20to%20be%20able%20to%20write%20SQL%20in%20which%20I%20specify%20the%20table%20name%20(msysobjects.name)%20and%20the%20table%20column.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1867725%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAccess%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1870630%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20column%20widths%20from%20MSysObjects%20using%20SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1870630%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F862886%22%20target%3D%22_blank%22%3E%40EarthSea%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20VBA%20is%20an%20option%20for%20you%2C%20you%20could%20get%20the%20column%20width%20with%20a%20function%20(that%20also%20can%20be%20used%20in%20a%20query)%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EPublic%20Function%20GetColumnWidth(sTable%20As%20String%2C%20sFieldname%20As%20String)%20As%20Long%0A%20%20Dim%20rs%20As%20dao.Recordset%0A%20%20Set%20rs%20%3D%20CurrentDb.OpenRecordset(sTable)%0A%20%20%0A%20%20GetColumnWidth%20%3D%20-1%20'Init%20the%20return%20value%20in%20case%20the%20requested%20field%20is%20not%20found%0A%20%20%0A%20%20Dim%20Field%20As%20dao.Field%0A%20%20rs.MoveFirst%0A%20%20'Loop%20over%20the%20table%20fields%20until%20the%20requested%20field%20is%20found%0A%20%20For%20Each%20Field%20In%20rs.Fields%0A%20%20%20%20If%20Field.Name%20%3D%20sFieldname%20Then%0A%20%20%20%20%20%20GetColumnWidth%20%3D%20Field.Size%0A%20%20%20%20%20%20Exit%20For%20'Step%20out%20of%20the%20For-loop%20since%20the%20requested%20field%20is%20found%0A%20%20%20%20End%20If%0A%20%20Next%0AEnd%20Function%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EPlace%20this%20function%20in%20a%20module%20and%20you%20can%20call%20it%20from%20a%20regular%20Access-query%3A%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20GetColumnWidth(%22tblBusiness%22%2C%20%22BusinessId%22)%20AS%20ColumnWidth%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3EWhere%20%22tblBusiness%22%20is%20the%20table%20name%20and%20%22BusinessID%22%20is%20the%20field%20name%2C%20which%20can%20be%20replaced%20with%20any%20table%20and%20field%20name%20you%20like.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMaybe%20this%20works%20for%20you.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EBest%20regards%2C%3C%2FP%3E%3CP%3ETieme%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1892224%22%20slang%3D%22en-US%22%3ERe%3A%20Getting%20column%20widths%20from%20MSysObjects%20using%20SQL%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1892224%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20Tieme%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20very%20much%20for%20the%20VBA%20code%2C%20I%20will%20keep%20that%20under%20my%20hat.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhat%20I%20am%20really%20looking%20for%20is%20a%20method%20that%20will%20work%20with%20an%20ACCESS%20database%20'out%20of%20the%20box'%20because%20I%20cannot%20necessarily%20expect%20my%20target%20audience%20to%20change%20their%20databases%20by%20adding%20functions.%20I%20really%20don't%20understand%20why%20column%20data%20in%20MSysObjects%20is%20not%20SQL-minable.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%20again.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

Hi All,

 

I work with an application that allows me to run SQL on the database to which it is connected. I want to be able to get column widths for tables like I can using the system table USER_TAB_COLUMNS in ORACLE.

 

Now, I know that I can do this:

 

SELECT lvprop FROM msysobjects WHERE flags = 0 AND type = 1

 

In the data returned I can see (in plain text) 'ColumnWidth' but the details are encoded in a manner I do not recognise and so cannot decode.

 

Could somebody please tell how I can mine column width data from msysobjects.lvprop?

 

My ultimate goal is to be able to write SQL in which I specify the table name (msysobjects.name) and the table column.

 

Thank you.

3 Replies
Highlighted

@EarthSea 

If VBA is an option for you, you could get the column width with a function (that also can be used in a query):

 

Public Function GetColumnWidth(sTable As String, sFieldname As String) As Long
  Dim rs As dao.Recordset
  Set rs = CurrentDb.OpenRecordset(sTable)
  
  GetColumnWidth = -1 'Init the return value in case the requested field is not found
  
  Dim Field As dao.Field
  rs.MoveFirst
  'Loop over the table fields until the requested field is found
  For Each Field In rs.Fields
    If Field.Name = sFieldname Then
      GetColumnWidth = Field.Size
      Exit For 'Step out of the For-loop since the requested field is found
    End If
  Next
  Set rs = Nothing
End Function

 

Place this function in a module and you can call it from a regular Access-query:

 

SELECT GetColumnWidth("tblBusiness", "BusinessId") AS ColumnWidth

 

Where "tblBusiness" is the table name and "BusinessID" is the field name, which can be replaced with any table and field name you like.

 

Maybe this works for you.

 

Best regards,

Tieme 

 

 

 

 

 

Highlighted

Hi Tieme,

 

Thank you very much for the VBA code, I will keep that under my hat.

 

What I am really looking for is a method that will work with an ACCESS database 'out of the box' because I cannot necessarily expect my target audience to change their databases by adding functions. I really don't understand why column data in MSysObjects is not SQL-minable.

 

Thank you again.

Highlighted
You won't find anything native. Such customization nearly always require VBA. Access is highly versatile but usually this is fine through advanced coding.

What is the end game here? What are you trying to achieve exactly? Maybe if we understand that better, we might be able to offer some suggestions?