M
moscat via AccessMonster.com
I previously submitted a post but was never answered. I must be because I
stated it worked but wanted input how it could be simplified. However, I
moved on without the simplification and I am moving forward writing it for
each field.
What I need to do is to have a Form where I set the default value of the
field in the table for several fields. I only found that the way to do it is
using ALTER TABLE...ALTER COLUMN. In my form, I am using an unbounded text
box and I named it 'CField1'. At the beginnning I tried the code shown below
and it works, but only if the text entered in CField1 has no spaces or
doesn't contain the " or ? characters. Is there a way to go around this. I
noticed that in the table, I can use the question mark as long as I have it
enclosed with double quotations as "?". In the case of the ", I am using it
as the inch symbol like 3/4". This is the code I am using:
Private Sub btnSubmit_Click()
Dim cnn As ADODB.Connection
Dim strSQL, strDef As String
Set cnn = CurrentProject.Connection
If Me.CField1 = "None" Then
strDefault = Empty
strSQL = "ALTER TABLE MyTbl ALTER COLUMN Field1 _
varchar(50) Default"
'strSQL = strSQL & strDef
cnn.Execute strSQL, , dbFailOnError
Set cnn = Nothing
ElseIf Not (IsNull(Me.CField1)) Then
strDefault = Me.CField1
strSQL = "ALTER TABLE MyTbl ALTER COLUMN Field1 _
varchar(50) Default "
strSQL = strSQL & strDef
cnn.Execute strSQL, , dbFailOnError
Set cnn = Nothing
End If
End Sub
Thank you for your help. I am stuck for now.
stated it worked but wanted input how it could be simplified. However, I
moved on without the simplification and I am moving forward writing it for
each field.
What I need to do is to have a Form where I set the default value of the
field in the table for several fields. I only found that the way to do it is
using ALTER TABLE...ALTER COLUMN. In my form, I am using an unbounded text
box and I named it 'CField1'. At the beginnning I tried the code shown below
and it works, but only if the text entered in CField1 has no spaces or
doesn't contain the " or ? characters. Is there a way to go around this. I
noticed that in the table, I can use the question mark as long as I have it
enclosed with double quotations as "?". In the case of the ", I am using it
as the inch symbol like 3/4". This is the code I am using:
Private Sub btnSubmit_Click()
Dim cnn As ADODB.Connection
Dim strSQL, strDef As String
Set cnn = CurrentProject.Connection
If Me.CField1 = "None" Then
strDefault = Empty
strSQL = "ALTER TABLE MyTbl ALTER COLUMN Field1 _
varchar(50) Default"
'strSQL = strSQL & strDef
cnn.Execute strSQL, , dbFailOnError
Set cnn = Nothing
ElseIf Not (IsNull(Me.CField1)) Then
strDefault = Me.CField1
strSQL = "ALTER TABLE MyTbl ALTER COLUMN Field1 _
varchar(50) Default "
strSQL = strSQL & strDef
cnn.Execute strSQL, , dbFailOnError
Set cnn = Nothing
End If
End Sub
Thank you for your help. I am stuck for now.