MS Access Issue - sql

Hello Stackoverflow community!
I have run into an issue and I'd love some advice.
I'm working with MS Access and I am trying to append two particular fields from one table to another; however this implementation is in a form and it gets a little complicated... So, I'll explain everything the best that I can
BACKGROUND INFORMATION:
First and fore most, I have two tables; one of which is a linked excel spread sheet from another directory (who is not willing to change any formatting what so ever, so I CANNOT make ANY changes to this file and it is being updated on a daily basis). This excel spreadsheet is very large and contains somewhere around 50 columns
The other table is not anywhere near as large but has around 20 columns and is meant to extract two columns from the excel spreadsheet (the first column and the third column). I'm trying to make a form for this database to be as user-friendly as possible and not many people in my office are familiar with the technicalities of Access queries and programming in VBA.
THE SITUATION:
On my form, the user will enter data into TextBoxA, from there they will click a button; this button will trigger a search through the linked excel spreadsheet for the data that was typed into TextBoxA. It will then copy the data from Field1 (which was the typed data) and Field3 and append these selected fields into the first two fields of the table in my Access Database. All of this is being done through a segment of VBA code
Private Sub CmdCloseForm_Click()
If IsNull(Me.TextBoxA) Or Me.TextBoxA = "" Then
MsgBox ("Field is empty, please try again!")
Else
Dim VendorNum As String
SearchingValue = Me.TextBoxA
Dim SQL As String
SQL = "INSERT INTO tbleRecord (Field1,Field2)" & _
"SELECT * " & _
"FROM tbleLinkedExcel " & _
"WHERE Field1 = '" & SearchingValue & "';"
DoCmd.RunSQL SQL
End If
End Sub
So the biggest issue here is that in Field1, and every time I try to run the code,
I receive an error; which I am assuming it is because of the space (unfortunately I cannot give the ACTUAL variable names out as it is confidential)
ERROR MESSAGE
The INSERT INTO statement contains the following unknown field name: 'FIELD 1'. Make sure you have typed the name correctly, and try the operation again.
The thing is, is that this 'FIELD 1' variable/name is not in my code, but in the linked excel spreadsheet (again, I am not able to change ANYTHING on this spreadsheet).
Thanks guys!

Related

How do I prevent duplicate entries in my Access database?

I am a first time coder with VBA and I am creating a database for data entry at a Psych Lab I work at. Currently the database is created, but I want to prevent duplicate entries from being put into the database (namely by have a code look for the participant number right after it is entered). I have been trying to fix this code for quite a while and I just recently hit a wall. It displays the correct error message when I enter the participant number, however it says that every number has been entered already (even though they actually haven't). Here is the code:
Private Sub Participant_Number_BeforeUpdate(Cancel As Integer)
Dim Participant_Number As Integer
Dim StLinkCriteria As Integer
If (Not IsNull(DLookup("[Participant_Number]", "Entry Log", "[Participant_Number] ='" & Me.Participant_Number.Value & "'"))) Then
MsgBox "Participant Number has already been entered in the database."
Cancel = True
Me.Participant_Number.Undo
End If
End Sub
Any help is greatly appreciated. I have never used VBA before and I am self-teaching how to code.
I guess your Participant_Number field is a number. You shouldn't enclose the criteria with single-quotes ', these are used with fields of text type. Try changing the criteria field from
"[Participant_Number] ='" & Me.Participant_Number.Value & "'"))) Then
into
"[Participant_Number] = " & Me.Participant_Number.Value))) Then
IF you have not used VBA you may try to do this by opening the table in Design view. This method is easy and a good choice. You may have a look here: https://support.office.com/en-us/article/Prevent-duplicate-values-in-a-field-b5eaace7-6161-4edc-bb90-39d1a1bc5576?ui=en-US&rs=en-US&ad=US&fromAR=1

Passing a query a parameter [Access 2016]

To make a longer story shorter:
I'm an Access noob, doing a quick-and-dirty conversion of a massive Excel spreadsheet into an Access database. Part of the requirements are to mimic some of the functionality of Excel, specifically, pulling data from a certain table and doing some basic calculations on it (sums, averages, etc.).
I've written a chain of queries to pull the data, count/sum it, etc., and have been testing them by using a manually-entered Parameter (i.e., the kind where the input box pops up and asks you to type a response). Now that I'm ready to drop these queries into a (sub)form, though, I have no idea how to automatically pass that parameter from a box in the form into the subform into the query.
Every query I've written uses a manually-entered Parameter named "MATCHNAME," which holds the name of an individual. In manual testing, if I enter this parameter on one query, all the queries it calls also get that value. So, I think I just need to figure out how to tell the top query what MATCHNAME actually is, and that'll take care of it.
Problem is, I don't know how to do that in Access. If it was any other programming language, I'd do something like "queryXYZ(MATCHNAME);", but I don't think I can do that in Access. Plus, since the values queryXYZ returns are all calculated, I'm not sure how to add an extra MATCHNAME field, nor how to actually make sure that gets read by the queries, nor how to make sure it gets passed down the chain. I've even tried creating a Parameter in design view, then trying to set up Link Master Fields, but the Parameter doesn't appear in that window.
I'd also like to re-run these queries whenever a new record is pulled up, but I'm not sure how to do that either--i.e., the numbers should be current for whatever record I'm looking at.
And, before we go there--I feel like a Relationship is out of the question, as the data itself is auto-generated, and is in rough enough shape to where I can't guarantee that any given key is wholly unique, and large enough (20k+) that, outside of writing a magical script, I can't assign a numerical key. However, I don't know much about Relationships in Access, so please prove me wrong.
(Is this all making sense?)
Do you have any suggestions for me--for how to make a subform read a field on the main form to run its queries on? Alternately, is there an easier way to do this, i.e., to bed SQL calls inside a form?
Thanks very much for your help...
You can use SQL as the recordsource of the subform in the property tab and use the afterupdate event of your matchname field to change yourform.recordsource = "Select * from table where filteredfieldname = & me.matchname & ";" . You can also use sql as the control source of form fields. To pass criteria to filter the subform using the whole table as the recordsource, add an event procedure to your field's after update event like this
`In the declarataions at the top
Global mtchnmfltr as string
Private Sub MATCHNAME_AfterUpdate()
'use the same procedure for Private Sub yourmainform_Current()
mtchnmfltr = "[yourfilterfield] = " & Chr(34) & me.matchname & Chr(34)
'if matchname is not text then just = "[yourfilterfield] = " & me.matchname
with me.subformname.form
.filter = mtchnmfltr
.filteron = true
end with
'Build your sql as a string for your sum avg fields etc. using mtchnmfltr in the where clause
me.yoursumfield.controlsource = "Select...where " & mtchnmfltr & ";"
'etc.
end sub
Or you could throw Matchname into a sql recordsource of the subform and add the function fields to the subform on the same on current and after update events
if me.newrecord = true then
me.dirty = false
end if
me.subform.form.recordsource = "Select Table.Matchname, sum(yourfield) as sumalias, _
(etc.) from yourtable where table.matchname = " & chr(34) & me.matchname & _
chr(34) & Group By table.matchname"
If you are storing your sums etc in a table you need to do it a bit different, since your controls controlsource are bound to fields.
dim strsqlsumfld as string
dim rs as dao.recordset
strsqlsumfld= "Select SUM.....AS sumfldalias where " & mtchnmfltr & ";"
set rs = currentdb.openrecordset(strsqlsumfld)
me.yoursumfield = rs("sumfldalias")
rs.close

MS Access SQL to copy part of a record displayed on a form, insert into same table as new record, and display the new record on a form

I have inherited a Microsoft Access 2010 database that is used to keep position/employee information in a single table. The table has an auto-number field for each record. The database has a form – ScreenB – that uses a query to pull and display in ScreenB a single record based on search criteria (Personnel Number, Last Name, Social Security Number, etc.) entered by the user. The record contains approximately 25 fields that show the information about the position as well as information about the person in the position. Of course, every so often a person will leave a position and someone else will be hired into the vacated position. My users would like a button on ScreenB that will, when clicked:
Copy the record information displayed in the form for just the 12 or so fields of position information being displayed on ScreenB.
Paste the information as a new record in the table.
Display the new record with the pasted information in the form.
Since the query pulls a single record, my thought was to create a new form – ScreenC – identical in all respects to ScreenB to display the new record. So, the button will copy the position information from the record displayed on ScreenB, paste it as a new record into the table, open ScreenC, jump to the end of the records in the table and display the new (last) record (which contains only the position information) on ScreenC, and close ScreenB, leaving the user with the position information only and a form that can be filled out with the employee information. This will preserve the information of the previous employee while in that position, which is required.
I’m not a trained programmer, so the code below is probably rife with errors, but it was the best I could come up with:
Dim strSQL as String
strSQL = "INSERT INTO [tblNewCurrent Emp] (numCostCenter, numPosition, txtTitles, txtClassCode. numGrade, txtPositionType, txtUnit1, txtFieldStaff, txtFedMatch, txtPositionLicReq, txtPositionOrigin, datPosAcquired, datPosLastVacated, datPosTransferred, txtOrigTitle, txtOrigClassCode, numOrigGrade, datPosReclassified) "
strSQL = strSQL & "SELECT numCostCenter, numPosition, txtTitles, txtClassCode. numGrade, txtPositionType, txtUnit1, txtFieldStaff, txtFedMatch, txtPositionLicReq, txtPositionOrigin, datPosAcquired, datPosLastVacated, datPosTransferred, txtOrigTitle, txtOrigClassCode, numOrigGrade, datPosReclassified from [tblNewCurrent Emp] "
strSQL = strSQL & "WHERE (numSystemNumber = " & Me.numSystemNumber & ")"
DoCmd.RunSQL (strSQL)
DoCmd.OpenForm frmScreenC
DoCmd.GoToRecord , , acLast
DoCmd.Close frmScreenB
The table is named “tblNewCurrent Emp”, which is why I placed it in brackets. numSystemNumber is the name of the auto-number field. All forms, queries and tables are in a single database/file. The field names in the code above are the actual names of the fields.
When I attempt to run the code, I get an error message that states, “Syntax error in INSERT INTO statement.” No other information, and no information in the Access Help system. I tried removing the brackets from the table name, and get the same error message. I would appreciate any input, thoughts, or suggestions. What is the error with the INSERT INTO statement? Is there another way to accomplish this task? Unfortunately, I can’t make structural changes to the database and since it is the backend for a number of other databases, changing the table name is not an option, either. Users access the database through the forms only. No datasheet or other views. Thanks.
EDIT: Thank you billmac1 and Remou. The "...,txtClassCode. numGrade,..." was the issue on the SQL. Once I corrected that, I used your suggestion to run the code in the Immediate Windows with the print.debug as well. Ran fine, so all that was left was to clean up the VBA code for opening and closing the forms. For anyone else attempting the same, I also set ScreenC to open to the last record in the Load property. It works perfectly now. Below is the finished code.
Dim strSQL As String
strSQL = "INSERT INTO [tblNewCurrent Emp] (numCostCenter, numPosition, txtTitles, txtClassCode, numGrade, txtPositionType, txtUnit1, txtFieldStaff, txtFedMatch, txtPositionLicReq, txtPositionOrigin, datPosAcquired, datPosLastVacated, datPosTransferred, txtOrigTitle, txtOrigClassCode, numOrigGrade, datPosReclassified) "
strSQL = strSQL & "SELECT numCostCenter, numPosition, txtTitles, txtClassCode, numGrade, txtPositionType, txtUnit1, txtFieldStaff, txtFedMatch, txtPositionLicReq, txtPositionOrigin, datPosAcquired, datPosLastVacated, datPosTransferred, txtOrigTitle, txtOrigClassCode, numOrigGrade, datPosReclassified FROM [tblNewCurrent Emp] "
strSQL = strSQL & "WHERE (numSystemNumber = " & Me.numSystemNumber & ")"
DoCmd.RunSQL (strSQL)
DoCmd.OpenForm "frmScreen C"
DoCmd.Close acForm, "frmScreen B"
Too new to this site to leave this one as a comment, but part of your SQL string caught my attention "...,txtClassCode. numGrade,...". There's a period and a space in both your field string and value string.
As Remou suggested, "...try a debug.print strSQL and paste the result into the query design window to check for problems." You might also find it beneficial to just take the SELECT portion of your statement and get it working, then try the INSERT portion.

Importing specific Access table data into Excel using Excel as the front end

I am trying to store and retrieve data that is entered into Excel into Access. I am an Access newbie and already have an Excel program on the front end, leading me to drive the import from Excel. I have successfully figured out how to save my Excel Named Range into the desired Access table, but now I need to figure out how to pull specific data from that Access table back into Excel. I know I can simply use the Get External Data feature from Excel to import the entire Access table into Excel, but I need to be able to only import a specific portion of the table into Excel based upon a predetermined set of parameters. Is this possible to do?
As a background to the program, basically it stores data from part number runs. Not only do I need to save new part runs into an Access database (which I figured out), but I also need to be able to pull previous part number runs from Access back into Excel to perform further analysis. That is why I need to figure out how to import only a specific portion of the table. I'm not sure what code, if any, I can post since I'm basically looking for code from scratch; but if there's any code you think you need from my Excel program I'm happy to provide it. Thanks for your help. Below is the code:
Sub GetSpecData()
Application.ScreenUpdating = False
'*******************************************************************************
'Open the connection to the data source.
Set Connection = New ADODB.Connection
Connection.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & "Data Source=\\Server2013\network_shared\QC SHARED\Databases\P&Q_Tracking_Data_Storage.mdb;"
'*******************************************************************************
'Create the new RecordSet.
Set Recordset = New ADODB.Recordset
With Recordset
'Define the appropriate Filter(s) and notify the user of the selection criteria.
Src = "Select * from Raw_Data where Tag = 'GHI' "
Src = Src & "or Tag = 'DEF' "
Src = Src & "or Tag = 'LMN'"
.Open Source:=Src, ActiveConnection:=Connection
'Write the field names.
For ODCol = 0 To .Fields.Count - 1
Tracking.Sheets("Selected Past Data").Range("B7").Offset(0, ODCol).Value = .Fields(ODCol).Name
Next
'Write the recordset.
Tracking.Sheets("Selected Past Data").Range("B7").Offset(1, 0).CopyFromRecordset Recordset
End With
Set Recordset = Nothing
Connection.Close
Set Connection = Nothing
'*******************************************************************************
'Create and format the table from the Recordset.
With Tracking.Sheets("Selected Past Data")
DataLastRow = .Range("A" & Rows.Count).End(xlUp).row
.ListObjects.Add(xlSrcRange, Range("B7:M" & DataLastRow), , xlYes).Name = "INC2tbl"
.ListObjects("INC2tbl").ShowTotals = True
End With
Application.ScreenUpdating = True
'*******************************************************************************
End Sub
After googling the suggestion in the comment below, I have a couple questions. First, the code above seems to filter access data by three keys: GHI, DEF, and LMN. Am I interpreting that correctly? Second, where it says "Select * from Raw_Data where Tag = 'GHI' ", since that's in quotes, that's not actual code that will be executed, correct? That's simply a prompt or something like it, correct?
SELECT * FROM TABLE; OUTPUT TO TABLEName.EXL
FORMAT sql;
First, the code above seems to filter access data by three
keys: GHI, DEF, and LMN. Am I interpreting that correctly?
Second,
where it says "Select * from Raw_Data where Tag = 'GHI' ", since
that's in quotes, that's not actual code that will be executed,
correct? That's simply a prompt or something like it, correct?
First and second questions relate closely, so I'll answer as one. The basic idea here is that there is a connection to the database you defined (in the Connection.Open statement). The connection itself does nothing then that; establish a connection. The actual communication with the database is done in a specific language, named SQL (there are many dialects, but for simplicity sake, call it SQL for now). So your code in in the VBA language, but the communication with the database is done in SQL. The VBA code has to produce SQL statements (and those are strings, so you need the quotes). That's what the Src variable holds; and SQL statement. In this case:
"Select * from Raw_Data where Tag = 'GHI' or Tag = 'DEF' or Tag = 'LMN'"
I think this is not the place to teach you SQL, but there is plenty information on the net about it. What this statement does is:
select all columns from table "Raw_Data", but only those rows of data that has 'GHI', 'DEF' or 'LMN' in the Tag column.
So to receive all the data from table "part_numbers", you would have to use:
"SELECT * FROM part_numbers;"
And if you need to import only columns "col_1" and "col_2", you would use:
"SELECT col_1,col_2 FROM part_numbers;"
And if you need to import only columns "col_1" and "col_2", and only rows with "part_id" lower than 1000, you would use:
"SELECT col_1,col_2 FROM part_numbers WHERE part_id < 1000;"
That's it really. Now if your recordset (think of it as an array that holds the data you asked for) has queried the database, you can loop it to parse the data. Something like:
Set Recordset = Connection.Execute(Src)
Do until Recordset.EOF
debug.print Recordset!col_1
Recordset.movenext
loop
Again, google something like: "vba excel adodb access", and you'll hit plenty examples.
After researching SQL code formatting more I have made more sense of the subject. Thanks for the help.

Problem with Excel SQL query code when it reads a closed workbook

Hi
I use the following code (part shown) which works great.
Basically I run a SQL query from excel which queries a closed workbook, extracts the info and puts it on the current opened sheet.
The problem is that one of the fields ("To") for some reason it always returns a value of nothing, even if every cell in that column has in fact information so I am not sure why it doesn't get the value.
All other columns are read perfectly.
Anyway knows why and whether there are some formatting (or other) specifics which affects SQL queries of this type?. There are no errors...simply the value is always nothing.
some code...
objRecordset.Open "SELECT * FROM [FILESAVED$] , objConnection, adOpenStatic, adLockOptimistic, adCmdText
for next_row=1 to objRecordset.count
Range("AF" & next_row) = objRecordset.Fields.Item("Validto")
Range("AG" & next_row) = objRecordset.Fields.Item("To")
Range("AO" & next_row) = objRecordset.Fields.Item("User Name")
objRecordset.movenext
next next_row
...more code
Thanks in advance
What kind of data is in the column? When you treat an Excel workbook as a database table, Excel infers the column type by matching it to whatever it infers about the majority of entries. (So if 60% look like numbers and 40% like text, it will treat them all as numbers.) This can cause errors, though it doesn't precisely match what you describe.