PC Review


Reply
Thread Tools Rate Thread

Can I set the DefaultValue of a field in a table directly?

 
 
Jim Thacker
Guest
Posts: n/a
 
      17th Apr 2009
If so, how?
--
Thanks, Jim
 
Reply With Quote
 
 
 
 
Roger Carlson
Guest
Posts: n/a
 
      17th Apr 2009
Put the value you want in the Default property of the table in Design View.

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/...UBED1=ACCESS-L


"Jim Thacker" <(E-Mail Removed)> wrote in message
news:EA9A8CC3-9776-499A-9F9F-(E-Mail Removed)...
> If so, how?
> --
> Thanks, Jim



 
Reply With Quote
 
Jim Thacker
Guest
Posts: n/a
 
      17th Apr 2009
Sorry Roger, I didn't make my question clear.
I want to change the DefaultValue of a table field when I change the value
of a textbox (using the AfterUpdate Event) in an unrelated form.
--
Thanks, Jim


"Roger Carlson" wrote:

> Put the value you want in the Default property of the table in Design View.
>
> --
> --Roger Carlson
> MS Access MVP
> Access Database Samples: www.rogersaccesslibrary.com
> Want answers to your Access questions in your Email?
> Free subscription:
> http://peach.ease.lsoft.com/scripts/...UBED1=ACCESS-L
>
>
> "Jim Thacker" <(E-Mail Removed)> wrote in message
> news:EA9A8CC3-9776-499A-9F9F-(E-Mail Removed)...
> > If so, how?
> > --
> > Thanks, Jim

>
>
>

 
Reply With Quote
 
Arvin Meyer MVP
Guest
Posts: n/a
 
      17th Apr 2009
Unless you open a table in Design View, either physically or in VBA code,
and save it, there is no way to change the DefaultValue of a field. The
problem is that as soon as you dirty a form with a new record, you must
first save that record before you can save the database, and no one else can
be in the database while you are saving it.

You can change the DefaultValue of a textbox on a form, but it won't "stick"
unless you save it in the form's design view.

I sense that there is an ulterior motive for your request, so it might be a
good idea to tell us what your are trying to accomplish. Perhaps there is a
better way to do it.
--
Arvin Meyer, MCP, MVP
http://www.datastrat.com
http://www.mvps.org/access
http://www.accessmvp.com


"Jim Thacker" <(E-Mail Removed)> wrote in message
news:C9FC87C3-9E1E-433C-8E75-(E-Mail Removed)...
> Sorry Roger, I didn't make my question clear.
> I want to change the DefaultValue of a table field when I change the value
> of a textbox (using the AfterUpdate Event) in an unrelated form.
> --
> Thanks, Jim
>
>
> "Roger Carlson" wrote:
>
>> Put the value you want in the Default property of the table in Design
>> View.
>>
>> --
>> --Roger Carlson
>> MS Access MVP
>> Access Database Samples: www.rogersaccesslibrary.com
>> Want answers to your Access questions in your Email?
>> Free subscription:
>> http://peach.ease.lsoft.com/scripts/...UBED1=ACCESS-L
>>
>>
>> "Jim Thacker" <(E-Mail Removed)> wrote in message
>> news:EA9A8CC3-9776-499A-9F9F-(E-Mail Removed)...
>> > If so, how?
>> > --
>> > Thanks, Jim

>>
>>
>>



 
Reply With Quote
 
Roger Carlson
Guest
Posts: n/a
 
      17th Apr 2009
Well, yes you can, but I generally don't advise it. Here are two routines:

Function SetAccessProperty(obj As Object, strName As String, _
intType As Integer, varSetting As Variant) As Boolean
Dim prp As Property
Const conPropNotFound As Integer = 3270

On Error GoTo ErrorSetAccessProperty
' Explicitly refer to Properties collection.
obj.Properties(strName) = varSetting
obj.Properties.Refresh
SetAccessProperty = True

ExitSetAccessProperty:
Exit Function

ErrorSetAccessProperty:
If Err = conPropNotFound Then

' Create property, denote type, and set initial value.
Set prp = obj.CreateProperty(strName, intType, varSetting)
' Append Property object to Properties collection.
obj.Properties.Append prp
obj.Properties.Refresh
SetAccessProperty = True
Resume ExitSetAccessProperty
Else
MsgBox Err & ": " & vbCrLf & Err.Description
SetAccessProperty = False
Resume ExitSetAccessProperty
End If
End Function



Sub ModifyDefaultValue(tablename As String, _
fieldname As String, _
fieldvalue As Variant)
Dim dbs As Database, tdf As TableDef, fld As Field
Dim blnReturn As Boolean

' Return reference to current database.
Set dbs = CurrentDb
' Return reference to table and field.
Set tdf = dbs.TableDefs(tablename)
Set fld = tdf.Fields(fieldname)
' Call SetAccessProperty function.
blnReturn = SetAccessProperty(fld, _
"DefaultValue", dbLong, fieldvalue)
' Evaluate return value.
If blnReturn = True Then
Debug.Print "Property set successfully."
Else
Debug.Print "Property not set successfully."

End If
End Sub


'You could call the preceding function with a procedure such as the
following:
Sub test()
Call ModifyDefaultValue("FieldFormatTable", "Field1", 100)
End Sub


--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/...UBED1=ACCESS-L



"Jim Thacker" <(E-Mail Removed)> wrote in message
news:C9FC87C3-9E1E-433C-8E75-(E-Mail Removed)...
> Sorry Roger, I didn't make my question clear.
> I want to change the DefaultValue of a table field when I change the value
> of a textbox (using the AfterUpdate Event) in an unrelated form.
> --
> Thanks, Jim
>
>
> "Roger Carlson" wrote:
>
>> Put the value you want in the Default property of the table in Design
>> View.
>>
>> --
>> --Roger Carlson
>> MS Access MVP
>> Access Database Samples: www.rogersaccesslibrary.com
>> Want answers to your Access questions in your Email?
>> Free subscription:
>> http://peach.ease.lsoft.com/scripts/...UBED1=ACCESS-L
>>
>>
>> "Jim Thacker" <(E-Mail Removed)> wrote in message
>> news:EA9A8CC3-9776-499A-9F9F-(E-Mail Removed)...
>> > If so, how?
>> > --
>> > Thanks, Jim

>>
>>
>>



 
Reply With Quote
 
Jim Thacker
Guest
Posts: n/a
 
      17th Apr 2009
I found the method!
1) I have a form that sets the value of BP in the TextBox named "BP"
2) Upon entering the value in the TextBox, I want that value to update the
DefaultValue of the BP field in Table "TAB"

Here is the Code:

Private Sub BP_AfterUpdate()
Dim BP_Val As Byte
Dim dbsST As Database
Set dbsST = CurrentDb
Dim currTable As TableDef

BP_Val = Form!BP

Set currTable = dbsST.TableDefs!TAB
currTable.Fields!BP.DefaultValue = BP_Val

End Sub
--
Thanks, Jim


"Arvin Meyer MVP" wrote:

> Unless you open a table in Design View, either physically or in VBA code,
> and save it, there is no way to change the DefaultValue of a field. The
> problem is that as soon as you dirty a form with a new record, you must
> first save that record before you can save the database, and no one else can
> be in the database while you are saving it.
>
> You can change the DefaultValue of a textbox on a form, but it won't "stick"
> unless you save it in the form's design view.
>
> I sense that there is an ulterior motive for your request, so it might be a
> good idea to tell us what your are trying to accomplish. Perhaps there is a
> better way to do it.
> --
> Arvin Meyer, MCP, MVP
> http://www.datastrat.com
> http://www.mvps.org/access
> http://www.accessmvp.com
>
>
> "Jim Thacker" <(E-Mail Removed)> wrote in message
> news:C9FC87C3-9E1E-433C-8E75-(E-Mail Removed)...
> > Sorry Roger, I didn't make my question clear.
> > I want to change the DefaultValue of a table field when I change the value
> > of a textbox (using the AfterUpdate Event) in an unrelated form.
> > --
> > Thanks, Jim
> >
> >
> > "Roger Carlson" wrote:
> >
> >> Put the value you want in the Default property of the table in Design
> >> View.
> >>
> >> --
> >> --Roger Carlson
> >> MS Access MVP
> >> Access Database Samples: www.rogersaccesslibrary.com
> >> Want answers to your Access questions in your Email?
> >> Free subscription:
> >> http://peach.ease.lsoft.com/scripts/...UBED1=ACCESS-L
> >>
> >>
> >> "Jim Thacker" <(E-Mail Removed)> wrote in message
> >> news:EA9A8CC3-9776-499A-9F9F-(E-Mail Removed)...
> >> > If so, how?
> >> > --
> >> > Thanks, Jim
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: Directly Reference a Field in the Table? BruceM Microsoft Access Form Coding 0 5th Jan 2007 08:37 PM
Re: Directly Reference a Field in the Table? Rick Brandt Microsoft Access Form Coding 0 5th Jan 2007 06:09 PM
Launch Word Document Directly from Access table OLE Field. =?Utf-8?B?ZmVkdXBmZWRtYW4=?= Microsoft Access VBA Modules 1 9th Apr 2006 10:45 AM
DefaultValue for field Joy Microsoft Access 5 11th Mar 2006 01:39 AM
DefaultValue for memo field =?Utf-8?B?QkxUaWJicw==?= Microsoft Access Form Coding 0 31st Jan 2006 11:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:59 AM.