If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
Allow Zero length property not working when using create table sql statement
Access 2000 will allow me to insert a blank into a field with the
Allow zero length property set to no. Here is what I did. Created a table in access with SQL. CREATE TABLE TestTable (String1 char(4),String2 char(3),String3 char(40),String4 char(13)) Went to the design view of the table in Access and verified that the "Allow zero length" property is "no". Created an insert query like this: INSERT INTO TestTable ( String1, String2, String3, String4 ) SELECT "Test2", '' , '' , '' Ran the query and I will get a row of data in the testtable. If I go into the design of the table and change the length of field "string4" from 13 to 15 and save my changes. If I re-run the insert query above I will get an access error "microsoft Access can't append all the records in the append query" due to a validation rule violation. What is going on? Is my create table incorrect? I need to create the table with SQL (external app actual creates the table, but problem can be reproduce in Access) Thanks in advance, |
#2
|
|||
|
|||
Allow Zero length property not working when using create table sql statement
Hi EK.
Access is not violating the Allow Zero Length property, but you have created a table with a different field type than you expected. In Access (JET), you would normally create text fields with the TEXT keyword rather than the CHAR keyword, i.e.: CREATE TABLE TestTable (String1 TEXT(4),String2 TEXT(3),String3 TEXT(40),String4 TEXT(13)); If you do that, everything works as expected. If you use the CHAR keyword, Access creates a *fixed-width* field. Then when you execute your insert query, Access does not insert zero-length strings, but fills the fields with spaces. You can see this if you open the table and look at the data: you can select the text (the spaces) in each field. You can also use ADOX or DAO to demonstratethat the field is fixed length when the table created using the CHAR keyword, and False for the table created using the TEXT keyword: Function ShowPropsADOX(strTable As String) Dim cat As New ADOX.Catalog Dim tbl As ADOX.Table Dim col As ADOX.Column Set cat.ActiveConnection = CurrentProject.Connection Set tbl = cat.Tables(strTable) For Each col In tbl.Columns Debug.Print col.Name, col.Properties("Fixed length") Next Set col = Nothing Set tbl = Nothing Set cat = Nothing End Function Function ShowPropsDAO(strTable As String) Dim db As DAO.Database Dim tdf As DAO.TableDef Dim fld As DAO.Field Set db = CurrentDb() Set tdf = db.TableDefs(strTable) For Each fld In tdf.Fields Debug.Print fld.Name, Debug.Print ((fld.Attributes And dbFixedField) 0) Next Set fld = Nothing Set tdf = Nothing Set db = Nothing End Function -- Allen Browne - Microsoft MVP. Perth, Western Australia. Tips for Access users - http://allenbrowne.com/tips.html Reply to group, rather than allenbrowne at mvps dot org. "EK" wrote in message om... Access 2000 will allow me to insert a blank into a field with the Allow zero length property set to no. Here is what I did. Created a table in access with SQL. CREATE TABLE TestTable (String1 char(4),String2 char(3),String3 char(40),String4 char(13)) Went to the design view of the table in Access and verified that the "Allow zero length" property is "no". Created an insert query like this: INSERT INTO TestTable ( String1, String2, String3, String4 ) SELECT "Test2", '' , '' , '' Ran the query and I will get a row of data in the testtable. If I go into the design of the table and change the length of field "string4" from 13 to 15 and save my changes. If I re-run the insert query above I will get an access error "microsoft Access can't append all the records in the append query" due to a validation rule violation. What is going on? Is my create table incorrect? I need to create the table with SQL (external app actual creates the table, but problem can be reproduce in Access) Thanks in advance, |
#3
|
|||
|
|||
Your are correct sir! I did notice that that the fields were filled
with spaces, but never knew why. Thanks for the explanation. "Allen Browne" wrote in message ... Hi EK. Access is not violating the Allow Zero Length property, but you have created a table with a different field type than you expected. In Access (JET), you would normally create text fields with the TEXT keyword rather than the CHAR keyword, i.e.: CREATE TABLE TestTable (String1 TEXT(4),String2 TEXT(3),String3 TEXT(40),String4 TEXT(13)); If you do that, everything works as expected. If you use the CHAR keyword, Access creates a *fixed-width* field. Then when you execute your insert query, Access does not insert zero-length strings, but fills the fields with spaces. You can see this if you open the table and look at the data: you can select the text (the spaces) in each field. You can also use ADOX or DAO to demonstratethat the field is fixed length when the table created using the CHAR keyword, and False for the table created using the TEXT keyword: Function ShowPropsADOX(strTable As String) Dim cat As New ADOX.Catalog Dim tbl As ADOX.Table Dim col As ADOX.Column Set cat.ActiveConnection = CurrentProject.Connection Set tbl = cat.Tables(strTable) For Each col In tbl.Columns Debug.Print col.Name, col.Properties("Fixed length") Next Set col = Nothing Set tbl = Nothing Set cat = Nothing End Function Function ShowPropsDAO(strTable As String) Dim db As DAO.Database Dim tdf As DAO.TableDef Dim fld As DAO.Field Set db = CurrentDb() Set tdf = db.TableDefs(strTable) For Each fld In tdf.Fields Debug.Print fld.Name, Debug.Print ((fld.Attributes And dbFixedField) 0) Next Set fld = Nothing Set tdf = Nothing Set db = Nothing End Function -- Allen Browne - Microsoft MVP. Perth, Western Australia. Tips for Access users - http://allenbrowne.com/tips.html Reply to group, rather than allenbrowne at mvps dot org. "EK" wrote in message om... Access 2000 will allow me to insert a blank into a field with the Allow zero length property set to no. Here is what I did. Created a table in access with SQL. CREATE TABLE TestTable (String1 char(4),String2 char(3),String3 char(40),String4 char(13)) Went to the design view of the table in Access and verified that the "Allow zero length" property is "no". Created an insert query like this: INSERT INTO TestTable ( String1, String2, String3, String4 ) SELECT "Test2", '' , '' , '' Ran the query and I will get a row of data in the testtable. If I go into the design of the table and change the length of field "string4" from 13 to 15 and save my changes. If I re-run the insert query above I will get an access error "microsoft Access can't append all the records in the append query" due to a validation rule violation. What is going on? Is my create table incorrect? I need to create the table with SQL (external app actual creates the table, but problem can be reproduce in Access) Thanks in advance, |
Thread Tools | |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Q1: How to create a boolean table field in code? | Jeff Conrad | General Discussion | 5 | August 11th, 2004 08:22 AM |
reset button, create excel, import table, send e-mail, create back-up | matthew nance | General Discussion | 0 | July 27th, 2004 06:52 PM |
SQL statement to populate unbound textbox | Bill Foley | Using Forms | 3 | June 21st, 2004 07:26 PM |
create table using access 2000 connecting to SQL Server 2000 | Chris | Running & Setting Up Queries | 0 | June 11th, 2004 06:41 PM |
Merge From a SQL Table, Called in .Net c# | sajecw | Mailmerge | 1 | June 3rd, 2004 03:43 PM |