Link to home
Start Free TrialLog in
Avatar of BadOscar
BadOscar

asked on

Add field to Access DB Table Help with vb6 code

I need to add some fields to an Access DB in vb6. The fields have spaces in the names one looks like this:

Field Name: Plate Library
Field Size: Integer
Decimal Places: Auto
Default Value: 0
Required: No
Indexed: No

Here is what I have so far this works great for text fields as long as there is not a space in the name.
I need to solve that problem and also add the ability to set the other properties of the field.
Anyone have ideas on how to solve either of these problems?

*********************************************************************************
Public Function CreateField(DatabaseName As String, _
   ByVal TableName As String, ByVal FieldName As String, ByVal FieldType As String) As Boolean

'DataBaseName is the file/path name of the database
'TableName is the name of the table
'FieldName is the name of the Field you want to create
'FieldSize is the size in characters of the Field you want to create
'Returns true if successful, false otherwise
If Dir(DatabaseName) = "" Then Exit Function
On Error GoTo errorhandler
Dim db As DAO.Database
Set db = Workspaces(0).OpenDatabase(DatabaseName, False, False, ";pwd=rabbit")
If Not TableExists(db, TableName) Then GoTo errorhandler
db.Execute "ALTER TABLE " & TableName & " ADD COLUMN " & FieldName & " " & FieldType
db.Close
CreateField = True
Exit Function
errorhandler:
If Not db Is Nothing Then db.Close
End Function
*********************************************************************************
Avatar of vinnyd79
vinnyd79

Have you tried:

db.Execute "ALTER TABLE " & TableName & " ADD COLUMN " & Chr$(34) & FieldName & Chr$(34) &  " " & FieldType
ASKER CERTIFIED SOLUTION
Avatar of vinnyd79
vinnyd79

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BadOscar

ASKER

you two were helpfull, I was just looking for a little more, but thanks