Sharing: Caption Properties of a Field object in an Access database read-write

Source: Internet
Author: User
Tags object chr access database access
The place to play "asterisk" in the article is to focus attention, because the author here has walked a detour, wasting an afternoon wandering in the MSDN. Now let's share it with you.

This article explains how to read and write this property in VBA. Unfortunately, the Caption property of the Field object is not an ADO native object, but a "Access property accessible by ADO", which describes two ways to access this property in the Help document, one that utilizes ADO, a DAO, where I say the result directly, Since the Field object is not a AccessObject object in ACCESS2003 and previous versions, there is no accessobjectproperties attribute, so we don't want to solve this problem in ADO, Now give the DAO Code solution

Subsetproperty (dbstempasdao.field,strnameasstring,_ bootempasstring) dimprpnewasdao.property DimerrLoopAsError ' Attempttosetthespecifiedproperty.

Onerrorgotoerr_property dbstemp.properties (strName) =bootemp OnErrorGoTo0 exitsub err_property:

' Error3270meansthatthepropertywasnotfound. Ifdbengine.errors (0). Number=3270then

' Createproperty,setitsvalue,andappendittothe ' propertiescollection.

Setprpnew=dbstemp.createproperty (Strname,_ dbtext,bootemp)

DbsTemp.Properties.AppendprpNew Resumenext Else

' Ifdifferenterrorhasoccurred,displaymessage. Foreacherrloopindbengine.errors

MsgBox "errornumber:" &errloop.number&vbcr&_ errloop.description Nexterrloop End EndIf

Endsub subdisplayclumcaption (Byvaltbnameasstring,byvalfldindexasinteger)

DIMDSETASDAO.TABLEDEF)//***** must use TableDef object

Dimiasintegerdimtmppropasdao.property//Forced to use DAO type

Dimfldasdao.field//Forced to use DAO type dimtmptxtasstring ' Onerrorresumenext

Dimmsgasstringdimcdbasdao.database//***** forced to use DAO type

Setcdb=currentdb//**** key, determining static references to the current database

Setdset=cdb. TableDefs (tbname)//***** must use TableDef object

Foreachfldindset.fields tmptxt=fld. Name setpropertyfld, "Caption", Tmptxt msg=msg fld. Properties ("Caption")

Msg=msg Chr (a) Chr nextfld msgboxmsgendsub

Here the code has two sub, one is SetProperty, to determine whether a field has a specified property, if not set, there will be the corresponding value assigned to the property, this code is almost entirely copy Ms Help document. The other is displayclumcaption, which is a demo code that sets its Caption property by field name for fields in the specified table. If necessary, you can modify the SetProperty to make him a read-only function that enumerates the caption properties for each field in the specified table. In Displayclumcaption code



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.