How to get data from linked table? - tsql

I'm new in VBA so i don't know how to get data from linked table in ms access, But i tried some code but it show this error
"Run time error '3219'
Invalid Operation
I already search this error everywhere but not find right answer.
Code
Dim db As DAO.Database
Dim db As DAO.Recordset
Set db = CurrentDb
Set rsHoliday = db.OpenRecordset("Holidays", dbOpenTable)
//Error Occur in this line

You are trying to assign a Table to a Recordset. Loose the dbOpenTable parameter from the code. It would compile just fine and do what you want. Unless you wish to open the table you need to use DoCmd.OpenTable

Related

Tables are empty in Query Builder when Saved SQL script is retrieved

I saved a query I created in the SQL Developer Query Builder.
When I retrieved the query, the linked Tables don't show the Column Names in the Query Builder.
The Tables are still linked/joined to one another and the query still runs, but the columns names are blank.
How do I get the Column Name to reappear?
I'm new to SQL Developer, so I'm not sure how to resolve this issue.
Any help would be greatly appreciated.
Searched through the menus for something that would restore the columns names.
Searched through help option on SQL Developers and nothing found relating to this issue.
I was very tempted to delete this post when I found the answer myself.
However, I think there are more rookies out there like me.
The resolution is to add a semi-colon ";" to the end of the statement in the Worksheet Tab.
Even though I created the query in the Query Builder Tab, SQL Developer creates the SQL Statement in the Worksheet Tab.
So for some reason after you save that Query that was created in the Query Builder Tab; when you retrieve the script, you must add ";" to the end of the SQL Statement in the Worksheet Tab or else you get the issue I encountered with the blank columns.

Deleting/Adding records on a subform that has a similar record source of the main form

I am trying to make a subform that works similarly to a splitform.
I would use splitform but, as far as I could tell, I can't filter only the results of the datasheet portion of the split form, and not the results of the rest.
See here...
The subformPartDetail only returns records where the OrderID is equal to the mainform's OrderID. There is no filter in the mainform so it returns all partDetail Records regardless of the OrderID. In this particular instance, the main form has 21 records to cycle where as the subform only has four.
An issue occured when I use the subform for adding or deleting records. When I try to use the main form to cycle through the records, the added ones were skipped and the deleted ones threw an error telling me that the record I was attempting to go to has been deleted.
I corrected it like this by putting these on the subform events...
Private Sub Form_AfterInsert()
Dim frm As Form
Dim rst As Recordset
Set frm = Forms!PartDetails
Set rst = frm.Recordset
rst.Requery
End Sub
Private Sub Form_Delete(Cancel As Integer)
Dim frm As Form
Dim rst As Recordset
Set frm = Forms!PartDetails
Set rst = frm.Recordset
rst.Requery
End Sub
But say I try to delete the record the mainform is displaying from the subform. That code does not work and will not requery. It throws an error when I cycle away from and then back to that record.
How can I fix this?
I know I can move the record from the deleted one before it deletes by using
rst.move
I don't know where to began for testing whether the record that is being deleted is the same one that the main form is displaying.
Maybe I'm not understanding the situation correctly, but there shouldn't be any need for VBA to achieve this. If the subform is using the Link Master Fields and the Link Child Fields with your subform, those IDs would work automatically. And if you have a relationship set up between the two tables, if you delete the Order record (and have Enforce Referential Integrity: Cascade Delete) selected, it should clear out the line items automatically to avoid orphan records.

Trying to design form to query and edit records Access

I'm trying to make an Access database where we can scan in a list of bar-codes, batch edit information for them, then save all the records. Later, they will need to be scanned again to batch edit more information such as shipping and order numbers.
Does anyone have suggestions on how to best design this? To start with, I've (1) made separate tables for "orders" and "parts" and (2) created a form of text boxes and a button attempting to apply this info to a part subform where records are created via the bar-code scanner. I'm a rather new Access designer. Thanks!
The questions are:
I'm not sure how to query these records via the bar code (Part_ID field) to apply order information to them later.
I can't currently update all new part records at once through my form.
Here's a little guide on how to do it. Let me know if you have any questions.
Dim db As DAO.Database
Dim Rs As DAO.Recordset
'Here is an example of a vba qarry in access. tblOrdersWhere is obviously your table.
'orderNum is a column name in your table pref a primary key
'txtOrderNum.Value is the value of a text box
'If you want just a simple select * from table you can put after db.OpenRecordset("tblOrders")
set rs = db.OpenRecordset("Select * from tblOrders Where orderNum like '*" & txtOrderNum.Value & "*';", dbOpenDynaset)
'This is how you update records in Access
'If you want to update it's Rs.Edit instead of Rs.AddNew
Rs.AddNew
Rs("OrderItem").Value = txtGroupName.Value
Rs("OrderTime").Value = txtGroupNum.Value
rs.Update

Get date and time from MS Access database using visual basic

Good day! I would like to ask what are the syntax in getting the date and time from the MS Access database? the name of the database is "DatabaseTest", the name of the table is "Test" and the name of the field in the table is "schedDateTime". Any help will be greatly appreciated. Thanks!
Assuming that you interested in the time and date that a particular record was created, the easiest thing you can do is create a field named something like "EntryDate" set the date type to "Date/Time". Go under the field properties and select Format: " General Date" then type =now() into the Default Value.
I think your question could use a little clarification. Do you need help parsing a Date/time field, or do you need help figuring out what sql-type query to use?
Private Sub Form_Load()
Dim db As Database
Dim recSet As DAO.Recordset
Dim searchID As Integer
' Set searchID to the ID you are looking for
searchID = 1
' Set up the database
Set db = CurrentDb
' Get the value from the database
Set recSet = db.OpenRecordset("SELECT [schedDateTime] FROM [Test] WHERE ([ID] =" & searchID & ")")
' Where textBox is the name of a text box in your form
[textBox] = recSet(0)
End Sub
In the above, I have shown how to retrieve a value from a database and store it in a form. If this is similar to what you want, I am sure you can adjust the code to your needs.

How to create a universal MS Access form?

I have a database that has a two tables which differ only in several fields so I thought of creating a single form for them and adding an option group (radio switch) witch would determine to which table the data from fields should go. The thing i I dont know how to hange the destination and some field should be hidden or at least on one but not another option.
This gave me an idea that for small databases this could be used to create universal form.
How to control data destination from form field in MS Access depending on option goup?
You can call different tables based on variables you create in a blank form. Here's an example if you use the table name in a combo box.
Dim var as String
Dim myR as Recordset
var = me.combo_box_name.column(0)
Set myR = CurrentDb.OpenRecordset (var, dbOpenDynaset)
'Code here based on variable, use an if statement if you want
Set myR = Nothing