.CreateField Method using a string var as the Name - ms-access

This is what I have so far with some extraneous code left out and some anonymous database and variable names:
Dim dB as DAO.Database
Dim tdf as DAO.TableDef
Dim newField As DAO.Field
Dim newFieldNameVar As String
newFieldNameVar = "Variable Name"
Set dB = CurrentDb
Set tdf = dB.TableDefs("PreExistingTable")
Set newField = tdf.CreateField(newFieldNameVar)
tdf.Fields.Append newField
Set newField = Nothing
Set tdf = Nothing
Set dB = Nothing
Where the aforementioned code exists in my function is not in a loop but the newFieldNameVar is a product of a loop, and so changes regularly based on question thread irrelevant conditions.
At the point the .Fields.Append line executes I get this run-time error: Invalid field data type. '3259' I am fairly new to programming in any form or fashion, but imagine that I might be using the wrong syntax for the variable used as the Name element of the .CreateField Method. Any ideas? Am I able to use a string variable as the Name in the .CreateField Method?
Thanks for the help in advance!

While Type is an optional parameter of the .CreateField method, each field does need a type before you can append it to the table, else Access will throw this error.
It's also a good practice to specify field size.
Specify your field type, e.g.:
Dim dB as DAO.Database
Dim tdf as DAO.TableDef
Dim newField As DAO.Field
Dim newFieldNameVar As String
newFieldNameVar = "Variable Name"
Set dB = CurrentDb
Set tdf = dB.TableDefs("PreExistingTable")
Set newField = tdf.CreateField(newFieldNameVar, dbText, 255) '255 character long text field
tdf.Fields.Append newField
Set newField = Nothing
Set tdf = Nothing
Set dB = Nothing

Related

VBA Access Object Type Returned For Combobox Field

How can I iterate a record set that returns a field of type field2?
Is there a way to tell how many objects are in the field2 type?
Let me describe the relevant aspects of my table:
The table fields has field NMR which contains a list of possible options a user can select in another table. In the Experiments table, the field NMR is a combobox with populates the options from the other table.
The way I do this is in the Experiments table design, I have set the field this way:
Now in one of my forms, I need to read the value in Experiments!NMR which can be a multiple selections allowed combobox. The recordset rs!NMR is of type Field2.
To get the values, you iterate using an integer (i.e. rs!NMR(0) would return the first selected option). The problem is I don't know how to get the field count and calling !NMR(i) where i is greater than the number of elements will invoke a Run time error '3265', Object doesn't exist in this collection.
Their exist a size method only returns the field width size (4?) and the documentation states this is the size of the data type within the field2 object.
There doesn't seem to be a count method associated with field2 as using !NMR.Count invokes runtime error 438, Object doesn't support this method.
Code:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim qry As String
qry = "SELECT * FROM Experiments"
Set db = CurrentDb
Set rs = db.OpenRecordset(qry, dbOpenSnapshot)
With rs
Do While Not .EOF
Dim i As Integer
For i = 0 to !NMR.Count ' or SOMETHING - this is the problem
' this is irrelevant, I need to know how to iterate the list
Next i
.MoveNext
Loop
End With
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
I've also tried logic control such as
Do While(!NMR(i) <> vbNullString) since the individual components are strings, but no luck. This issues the same 3265: Item isn't found error. Same for a loop with this check Do While Not IsNull(!NMR(i))
Is there a way to tell how many objects are in the Field !NMR?
You need to assign the complex Field2 to a Recordset2 object and loop through it.
Sub Whatever()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rsComplex As DAO.Recordset2
Dim qry As String
qry = "SELECT * FROM Experiments"
Set db = CurrentDb
Set rs = db.OpenRecordset(qry, dbOpenSnapshot)
Do While Not rs.EOF
Set rsComplex = rs("NMR").Value
rsComplex.MoveLast
rsComplex.MoveFirst
Dim i As Integer
For i = 0 To rsComplex.RecordCount - 1 ' Asker modified
Debug.Print rsComplex(0)
rsComplex.MoveNext
Next i
rsComplex.Close
Set rsComplex = Nothing
rs.MoveNext
Loop
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
End Sub

Setting a query to a recordset

I am trying to set a recordset that keeps giving me the error message - "Runtime Error 3061 Too few parameters. Expected 1".
The query checks to see what value is in a combo box and then returns two fields. This is the VBA code I tried -
Dim db As Database
Dim rst As Recordset2
Dim field As field
Dim n As Integer
Dim Qno As Integer
Dim sqlstr As String
Set db = DBEngine(0)(0)
Set rst = db.OpenRecordset("Get_Questions", dbOpenDynaset)
This is the SQL behind the Get_Questions Query -
SELECT Question_Lt.Qnumber, Question_Lt.Questions, Question_Lt.Freq
FROM Question_Lt
WHERE (((Question_Lt.ClientCd)=[Forms]![TestForm]![CmClient]));
Use your query as a QueryDef object, supply the parameter value, and then use its OpenRecordset method to populate your recordset.
'Set rst = db.OpenRecordset("Get_Questions", dbOpenDynaset)
Dim qdf As DAO.QueryDef
Set qdf = db.QueryDefs("Get_Questions")
qdf.Parameters(0).Value = [Forms]![TestForm]![CmClient]
Set rst = qdf.OpenRecordset(dbOpenDynaset)
i ve to few reputation points to comment your question. But what ist the SQL Statement exactly. It seems to me that your ' ' are not set but i don' t know
Im Just guessing: Sql Script for Get_Questions should be something like that. I don' t know the parameter "Get_Questions" is it a query-name?
sqlStr =
"SELECT...
FROM Question_LT
WHERE Client.Cd = " & [Forms]![TestForm]![CmClient]

how to fill a recordset in vba in a module behind a db

I have tried many ways to get the job done. I am inexperienced with the
Access VBA.
I think the problem is how to set the current database. The code is in a module from another db as the current db. I have paste the code here in a module behind the
currentdb that also gives the same error. I have looked after very much questions.
It must be simple. But I don't see the answer.
Private Sub project()
Dim projectnamen As DAO.Database
Dim strSQL As String
Dim rs As DAO.Recordset
Dim strdbName As String
Dim strMyPath As String
Dim strdb As String
Dim accapp As Object
Path = "c:\GedeeldeMappen\programma en bestanden stiko"
strdbName="projektnamen.accdb"
strMyPath = Path
strdb = strMyPath & "\" & strdbName
'make the db "projectnamen"current. Perhaps this is possible with set??
Set accapp = CreateObject("Access.Application")
accapp.OpenCurrentDatabase (strdb)
'fieldname is naam_van_het_project
'tablename is projectnaam
strSQL = "SELECT All naam_van_het_project FROM projectnaam;"
'here i get an error "can't find the object Select All naam_van_het_project
'FROM projectnaam" error 3011
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenTable)
rs.MoveFirst
Do While Not rs.EOF
MsgBox (rs)
rs.MoveNext
Loop
End Sub
I think you want to run that query against the db which you opened in the new Access session, accapp.
The CurrentDb method is a member of Application. So qualify CurrentDb with the object variable name of that other application session.
'Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenTable)
Set rs = accapp.CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
Note OpenRecordset won't let you use dbOpenTable with a query. So I arbitrarily chose dbOpenSnapshot instead. If that's not what you want, substitute a different constant from the RecordsetTypeEnum Enumeration (see Access help topic for details).

How to set rich text property when creating memo field in DAO?

I'm creating a table with a memo field in DAO.
However, I don't know how to set/create the rich text property for the field.
The following code gives a run time error:
Sub CreateTable()
Dim db As DAO.Database
Dim tdf As DAO.TableDef
Dim fld As DAO.Field2
Set db = CurrentDb
' Create table
Set tdf = db.CreateTableDef
tdf.Name = "myTable"
Set fld = tdf.CreateField("memo_field", dbMemo)
fld.Properties("TextFormat").Value = acTextFormatHTMLRichText '<- getting error here
tdf.Fields.Append fld
db.TableDefs.Append tdf
db.TableDefs.Refresh
Application.RefreshDatabaseWindow
Set fld = Nothing
Set tdf = Nothing
Set db = Nothing
End Sub
Somebody in a forum suggested something like this,
fld.Properties.Append fld.CreateProperty("TextFormat", dbByte, acTextFormatHTMLRichText)
but I was not able to implement this.
I guess I just go the syntax wrong. What would be the correct way to implement this?
Thanks for your help!
You may have trouble appending a property to a field that is not yet appended to a TableDef.
Try:
Set fld = tdf.CreateField("memo_field", dbMemo)
tdf.Fields.Append fld
tdf.Fields(fld.Name).Properties.Append fld.CreateProperty("TextFormat", dbByte, acTextFormatHTMLRichText)

Access 2010 VBA to put one field from a query in a variable

In Access 2010 I need to be able to click a command button that will run a query that returns a small two field recordset. Then put the second field in that recordset into a string variable.
This string variable is a link to a word document on the network. the second part of the code will then open the word document.
Any help is GREATLY appreciated.
I am getting the Error: "Object variable or With block variable not set"
My Code looks like this:
`Option Compare Database
Private Sub cmdCESpec_Click()
On Error GoTo Err_cmdCESpec_Click
Dim db As Database
Dim rs As DAO.Recordset
Dim s As String
Dim specSheet As String
s = "SELECT p.PartNum, p.CE_SpecSheet FROM tblParts p WHERE p.PartNum = '" & [Forms]![frmSpecSheet]![cboPartNum] & "'" 'Chooses the correct Spec Sheet.
Set rs = db.OpenRecordset(s)
specSheet = rs.Fields("CE_SpecSheet") 'Chooses the Spec Sheet Field
rs.Close
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
With oApp
.Documents.Open (specSheet)
End With
Exit_cmdCESpec_Click:
Exit Sub
Err_cmdCESpec_Click:
MsgBox Err.Description
Resume Exit_cmdCESpec_Click
End Sub
After dissecting your code.. this:
"..WHERE (((tblParts.PartNum)=[Forms]![frmSpecSheet]![cboPartNum]));"
embeds the words Forms!etc into your sql-statement, it doesn't insert the combo-box value into the statement.
You need to take the form reference out of the string:
Dim db As Database
Dim rs As Recordset
Dim s As String
Dim specSheet As String
s = "SELECT tblParts.PartNum, tblParts.CE_SpecSheet FROM tblParts WHERE " _
& "tblParts.PartNum=" & [Forms]![frmSpecSheet]![cboPartNum]
'Chooses the Correct Spec Sheet
Set db = CurrentDb
Set rs = db.OpenRecordset(s)
specSheet = rs.Fields("CE_SpecSheet")
'Chooses the Spec Sheet Field
rs.Close
Dim oApp As Object
Set oApp = CreateObject("Word.Application")
oApp.Visible = True
With oApp
.Documents.Open ("specSheet")
End With
If the combobox's value is text then you'll need to also surround this with apostrophes.
You also don't need to SELECT the field tblParts.PartNum, you can just refer to it in the WHERE clause.
You haven't posted much information but what I gather you're looking for is Recordset
Dim db As Database
Dim rs As Recordset
Dim s As String
Dim myString As String
s = "SELECT * FROM myTable1;" 'Replace with the SQL you need
Set db = CurrentDb
Set rs = db.OpenRecordset(s)
myString = rs.Fields("myFieldName1") 'Replace myFieldName with the appropriate field name
rs.Close