473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Set Unicode Compression in code

I am writing some vba to create a new field if it is not already there.
Code follows.

The code I have so far, creates the new field in the table and sets the
AllowZeroLength property = true.

'Start code
Private Sub AddNewField_Click()

On Error GoTo ErrAddNewField_Click
Dim db As DAO.Database
Dim fld As DAO.Field
Dim tdf As DAO.TableDef

Set db = Application.CurrentDb
Set tdf = db.TableDefs("tblTestCode")
Set fld = tdf.CreateField("MyNewField1", dbText, 150)

With fld
.AllowZeroLength = True
End With

With tdf.Fields
.Append fld
.Refresh
End With

Set fld = Nothing
Set tdf = Nothing
Set db = Nothing

Exit Sub

ErrAddNewField_Click:
MsgBox Err.Description
Resume Exit_ErrAddNewField_Click

End Sub
'End code

I have two problems.

1. How do I create this new field ONLY if it does not already exist.
2. This code defaults to Unicode Compression = No for a text field and I
would like it to be Yes. How do I do this?

dixie
Nov 13 '05 #1
2 6249
> I have two problems.

1. How do I create this new field ONLY if it does not already exist.
2. This code defaults to Unicode Compression = No for a text field and I would like it to be Yes. How do I do this?

dixie


Just two? Consider yourself lucky!

1. you check to see if the field exists... I got the TableExists code
from Accessweb and then wrote the FieldExists function by tweaking
that... The debug statements are just so you can see where the
"search" fails - whether the table or the field doesn't exist.

'******************** Code Start ************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
'
Function fExistTable(strTableName As String) As Boolean
Dim db As Database
Dim i As Integer
Set db = DBEngine.Workspaces(0).Databases(0)
fExistTable = False
db.TableDefs.Refresh
For i = 0 To db.TableDefs.Count - 1
If strTableName = db.TableDefs(i).Name Then
'Table Exists
fExistTable = True
Exit For
End If
Next i
Debug.Print "Table exists: " & fExistTable
Set db = Nothing
End Function
'******************** Code End ************************

Function fExistField(strTableName As String, strFieldName As String) As
Boolean
On Error GoTo ErrHandler

Dim db As Database
Dim tdf As TableDef
Dim i As Integer

If fExistTable(strTableName) = True Then
Set db = DBEngine.Workspaces(0).Databases(0)
Set tdf = db.TableDefs(strTableName)

For i = 0 To tdf.Fields.Count
If strFieldName = tdf.Fields(i).Name Then
'Field Exists
fExistField = True
Exit For
End If
Next i

Set tdf = Nothing
Set db = Nothing
Else
fExistField = False
End If
Debug.Print "Field exists: " & fExistField

Exit Function

ErrHandler:
If Err.Number = 3265 Then
Debug.Print "Field exists: " & fExistField
fExistField = False
Err.Clear
End If

End Function

Nov 13 '05 #2
Its a lot of code for such a teensy weensy thing :>)

Actually, I already use the If Exists table form of the code in other areas.

If I just put On Error Resume Next at the top of the sub, would that not
work just as well? Because if the field already exists, it will create an
error? It appears to work, but I know it is not good practice and perhaps
there is a down side I cannot see.

Any help with number 2 - setting the Unicode Compression = Yes. I don't
really think it is that important, but I am told all text fields should be
set that way.

dixie

<pi********@hotmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have two problems.

1. How do I create this new field ONLY if it does not already exist.
2. This code defaults to Unicode Compression = No for a text field

and I
would like it to be Yes. How do I do this?

dixie


Just two? Consider yourself lucky!

1. you check to see if the field exists... I got the TableExists code
from Accessweb and then wrote the FieldExists function by tweaking
that... The debug statements are just so you can see where the
"search" fails - whether the table or the field doesn't exist.

'******************** Code Start ************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
'
Function fExistTable(strTableName As String) As Boolean
Dim db As Database
Dim i As Integer
Set db = DBEngine.Workspaces(0).Databases(0)
fExistTable = False
db.TableDefs.Refresh
For i = 0 To db.TableDefs.Count - 1
If strTableName = db.TableDefs(i).Name Then
'Table Exists
fExistTable = True
Exit For
End If
Next i
Debug.Print "Table exists: " & fExistTable
Set db = Nothing
End Function
'******************** Code End ************************

Function fExistField(strTableName As String, strFieldName As String) As
Boolean
On Error GoTo ErrHandler

Dim db As Database
Dim tdf As TableDef
Dim i As Integer

If fExistTable(strTableName) = True Then
Set db = DBEngine.Workspaces(0).Databases(0)
Set tdf = db.TableDefs(strTableName)

For i = 0 To tdf.Fields.Count
If strFieldName = tdf.Fields(i).Name Then
'Field Exists
fExistField = True
Exit For
End If
Next i

Set tdf = Nothing
Set db = Nothing
Else
fExistField = False
End If
Debug.Print "Field exists: " & fExistField

Exit Function

ErrHandler:
If Err.Number = 3265 Then
Debug.Print "Field exists: " & fExistField
fExistField = False
Err.Clear
End If

End Function

Nov 13 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JJ | last post by:
Hi, usually, I'm not using MS servers, but I have a big problem with a Access table. I should create a web application for a Historical Dipartment. They have create a populated a Access...
5
by: Bill Grigg | last post by:
I am attempting to set the UnicodeCompression property via code. The following line "appears" to work: tdf.Fields("State").Properties("UnicodeCompression").Value = True I can see the boolean...
4
by: JJ | last post by:
Hi, usually, I'm not using MS servers, but I have a big problem with a Access table. I should create a web application for a Historical Dipartment. They have created a populated a Access...
2
by: deko | last post by:
Is it best practice to set Unicode Compression to "No" for memo fields in a table? What about text fields? According to the VB help entry: "Data in a Memo field is not compressed unless it...
4
by: Bob Sillett | last post by:
This is one of those issues that doesn't clearly fit into a single category as it's both a C# and a database (mySql) issue. I want to INSERT and SELECT text in UTF8 from a C# application to...
0
by: jazaret | last post by:
I've been having a hard time getting the benefits that Unicode Compression offers (2003 Access). I've got a test database that I'd like to set the Unicode Compression for the fields. For this test...
18
by: Chameleon | last post by:
I am trying to #define this: #ifdef UNICODE_STRINGS #define UC16 L typedef wstring String; #else #define UC16 typedef string String; #endif ....
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
20
by: chance | last post by:
Hello, I want to add compression to a memory stream and save it in an Oracle database. This is the code I have so far: //save the Word document to a binary field, MemoryStream dataStream = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.