I would like to be able to see a line of text in a textbox from an access database.
I know how to write and delete data from the database through visual basic code, but how can you read data?
If for example, I have something that says "Hi" saved in my database. How can I use a line of code in visual basic to read "Hi" and then show it in a textbox or simply in the console app?
Sub SayHi()
Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strCity As String
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset("SELECT Foo FROM tblCustomers WHERE Foo = " & "'Hi'")
'Begin row processing
Do While Not rst.EOF
MsgBox rst!Foo
Loop
'Cleanup
rst.Close
Set rst = Nothing
End Sub
Related
first off: I'm not an expert, I'm pretty much a noob at VBA.
onto my question:
So I'm using a Recordset in VBA, if the Recordset I open is a table or a simple query, it works,
but I have another query that I want to base that recordset on, but when I do that, it doesn't work.
The query I'm trying to use with the Recordset basically contains a field "user" that changes based on the textbox of a form. Is it not possible to open that query as recordset because it depends on the textbox of a form or am I missing something in the code?
Here my code for reference:
Dim db As Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("tblExample")
Do Until rs.EOF
Debug.Print rs.Fields("FieldExample")
rs.MoveNext
Loop
rs.Close
db.Close
Set rs = Nothing
Set db = Nothing
I also tried replacing "tblExample" with an SQL string that is basically the same as the query I'm trying to use, with no success. The query itself does work without any problems though.
The query I'm trying to use (shortened version):
SELECT Inventar.nutzName FROM Inventar WHERE (((Inventar.nutzName) Like "*" & [Formulare]![frmInventarNutzerFilter]![cbxNutzer] & "*"))
Your code cannot "see" the parameter value, so you will have to pass this explicetly:
Dim db As DAO.Database
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
Set db = CurrentDb
Set qd = db.QueryDefs("YourQuery")
qd.Parameters(0).Value = [Forms]![frmInventarNutzerFilter]![cbxNutzer]
Set rs = qd.OpenRecordset()
Do Until rs.EOF
Debug.Print rs.Fields("FieldExample")
rs.MoveNext
Loop
rs.Close
qd.Close
db.Close
Set rs = Nothing
Set db = Nothing
I'm sure I'm making a simple mistake. I have a SQL Server with tables that I have linked in MS Access. I am trying to run a SQL query in Form OnLoad to populate a text box based on a value in my form.
Below is my code. The field I want returned TE is varchar in MSSQL and short text in Access. I am getting a 'Type Mismatch' on the textbox. When I try outputting to a MsgBox I get:
'rst!TE not in collection'.
TN and TN_1 are smallint, in Access they are Number.
Dim rst As DAO.Database
Set rst = CurrentDb
rst.OpenRecordset "SELECT dbo_STCH.TE FROM dbo_STCH RIGHT JOIN dbo_SCVR ON dbo_STCH.TN = dbo_SCVR.TN_1 WHERE dbo_SCVR.TN_1=99;"
Me.Text22.Text = rst!TE
rst.Close
Set rst = Nothing
You are conflating DAO recordsets and databases and hence the message is correct as the item is not found in collection. Simply initialize rst as a recordset and assign it to OpenRecordset call.
Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("SELECT dbo_STCH.TE FROM dbo_STCH" _
& " RIGHT JOIN dbo_SCVR ON dbo_STCH.TN = dbo_SCVR.TN_1" _
& " WHERE dbo_SCVR.TN_1=99;")
Me.Text22.Text = rst!TE
rst.Close
Set rst = Nothing
You have to use rst.MoveFirst to move to the first record of the opened recordset before reading any values from it.
Also as the answer above you have to Set rst = CurrentDb.OpenRecordset
i am trying to copy a collection database in vba to access 2010 database, below is a code that works. But what i want to know is if there is some easier or quicker way of doing this especially when i am going to have large no or fields and records.
Dim plist As New partlist 'plist is a class
Dim plcol As New Collection
Sub DBInsert1()
' this function will add add 2 records from a collection to access database
Dim DB As DAO.Database
Dim RS As DAO.Recordset
plist.itemno = "1"
plist.itemname = "one"
plcol.Add plist
Set plist = Nothing
plist.itemno = "2"
plist.itemname = "two"
plcol.Add plist
'above plcol collection has a set of info
' open database
Set DB = DAO.OpenDatabase("D:\tblImport.accdb")
' open table as a recordset
Set RS = DB.OpenRecordset("Table1")
For Each plist In plcol
' add a record to the recordset
RS.AddNew
RS.Fields("itemno") = plist.itemno
RS.Fields("itemname") = plist.itemname
' write back recordset to database
RS.Update
Set plist = Nothing
Next
' important! cleanup
RS.Close
' forget to close the DB will leave the LDB lock file on the disk
DB.Close
Set RS = Nothing
Set DB = Nothing
End Sub
you could try an ADO connection to the DB www.connectionstrings.com and https://support.microsoft.com/en-us/kb/168336
CONNECTION.execute "INSERT INTO Table1 (itemno,itemname) values('" & plist.itemno & "','" & plist.itemname & ')"
Hi Maybe someone can enlighten me here. Using VBA code and I want to include a check for zero record count from a query and it now fails at the code I added where it was fine before. Code below shows set up:
Sub MySub()
Dim db As DAO.Database
Set db = CurrentDb()
Dim qdf As DAO.QueryDef
Dim sqlQry As String
Dim rst As DAO.Recordset
sqlQry = "My Query String"
If DLookup("Name", "MSysObjects", "Name= 'QueryName1'") <> "" Then
Set qdf = CurrentDb.QueryDefs("QueryName1")
qdf.SQL = sqlQry
Else
Set qdf = CurrentDb.CreateQueryDef("QueryName1", sqlQry)
End If
Set rst = db.OpenRecordset(qdf.Name) << FAILS HERE
If rst.RecordCount <> 0 Then
DoCmd.OpenQuery (qdf.Name)
Else
MsgBox "no data returned"
Exit Sub
End If
Set qdf = Nothing
Set db = Nothing
End Sub
It fails on the line shown above with run "time error 3219 Invalid Operation". the only thing I changed is added an object reference for business objects for another part of the code, not this part. It ran fine before and only other change is start of a new sub below this code but running only this bit code (not the whole module) in debug mode it came up with the error.
Below is the code to fill a list box in a VBA application :
Private Sub Form_Open(Cancel As Integer)
''#Populate list box control.
Dim cnn As ADODB.Connection
Dim strSQL As String
Dim rst As ADODB.Recordset
Dim strList As String
On Error GoTo ErrHandler
''#Use DSN to Northwind.
''#Modify connection and connection string as needed.
Set cnn = New ADODB.Connection
cnn.Open "DSN=NorthwindExample"
strSQL = "SELECT * FROM Shippers"
Set rst = New ADODB.Recordset
rst.Open strSQL, cnn
strList = rst.GetString(adClipString, , ";", ",")
Debug.Print strList
Me.lstShippers.RowSource = strList
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing
Exit Sub
ErrHandler:
MsgBox Err.No & ": " & Err.Description, vbOKOnly, "Error"
Set rst = Nothing
Set cnn = Nothing
End Sub
I need to know what i need to put as DSN string? Where will I get the info?
What is adClipString here in this code?
Is there any option to populate list control without using DSN connection object since I am taking the values from the same access table?
Here is a link that contains the different connection strings for Access:
http://www.connectionstrings.com/access
Something like this should work: Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
Im not sure what adClipString is, it could be an undeclared variable or database column?
Matt
Here is the info on adClipString.
Basically, GetString method gets the content of the entire recordset into a string variable where columns will be separated by ";" and rows will be separated by "," (as per your code).
Regarding DSN - see Start -> Settings -> Control Panel -> Administrative Tools -> Data Sources (ODBC). One of the tab (I guess System DSN) is where ODBC based data source can be created and are listed.