Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I am a novice programmer attempting to build a database for work in Access 2010 in an effort to better manage our data. Our filing system involves documents nested under one another in various branching subjects. The branches are all varying lengths, in my database each record always has a collection number, series number, and file unit number but there are several sub-series numbers that they may or may not have depending on how many subjects branch off of the previous subject. All of this information collects in a File Unit table.
I'm working on setting up a search form with a combobox for collection number, series number, each possible sub-series number, and then file unit number, so that from the first combobox the user chooses their collection number which then triggers a query that fills the series combobox with all series numbers available for that collection number, and so on and so forth down the line. My problem is with the file unit number combobox.
I need the file unit number combobox to fill with options after the series number is selected and update with each subsequent selection of sub-series number to only display file unit numbers from the file unit table that match the selected values and have null values for sub-series comboboxes that have nothing selected.
I've written a sub procedure that calls after the user exits each combobox when certain conditions are met. In the sub procedure, if then statements check each combobox, if there's a value it's stored in a corresponding string variable and if not the variable is set to IS NULL.
This is the code I'm using to fill the file unit number combobox:
File_Unit_Number.RowSourceType = "Table/Query"
File_Unit_Number.RowSource = "SELECT File_Unit.File_Unit_Number,
File_Unit.File_Unit_Name FROM File_Unit WHERE
(((File_Unit.Collection_Number) " & cmbCollection & " AND
(File_Unit.Series_Number) " & cmbSeries & "
AND(File_Unit.Subseries_1_Number) " & cmbSubseries_1 & "
AND(File_Unit.Subseries_2_Number) " & cmbSubseries_2 & "
AND(File_Unit.Subseries_3_Number) " & cmbSubseries_3 & "
AND(File_Unit.Subseries_4_Number) " & Subseries_4 & "
AND(File_Unit.Subseries_5_Number) " & cmbSubseries_5 & "));"
I've run tests to make sure that my if-then statements are triggering correctly and that the SQL string is printing correctly. When I remove the fields that have null values (ie. delete everything except series_number) from the WHERE part of the statement and test then items populate my file unit combobox. Problem is these numbers are all of the file unit numbers regardless of value in all of the sub-series columns.
What am I doing wrong? And is there an easier way to get the results I need?
In your code there are some weak points (and do what sstan suggested: Check the generated code before the execution!)
What is in your variables, if the value is not null? If I understand this correctly, you store the text "IS NULL" in your variables. If there is a value, your variables should contain something like "='SomeValue'" (Attention with strings, which must be in quotes, and formats like . or , in decimals or with datetime literals)
The paranthesis should include the full expression AND (FileUnit.Subseries_4_Number=123). You might let them away entirely as there are only ANDs.
And check for the blanks:
AND(File_Unit.Subseries_1_Number) " & cmbSubseries_1 & "
AND(File_Unit.Subseries_2_Number) " & cmbSubseries_2 & "
...might result in
AND(File_Unit.Subseries_1_Number) ISNULLAND(File_Unit.Subseries_2_Number) 123
(Look at the paranthesis, the missing blank before the AND and the missing = if your variable carries the value only.
UPDATE Beware of sql injection!
Anyway SQL statements with values should not be created by concatenating strings but should use parameters...
Sorry to waste anyone's time! I sorted out the answer on my own. It turned out that some of the fields were not NULL but empty strings. I changed the string that was being saved to the variable when there was no value to:
[table].field IS NULL or [table].field = ""
Related
This is my first post here. Kind of a newbie at Access and I've searched forums to answer my question and tried using similar answers to get my macro working, but I have a syntax error.
I want to click the details button on one form (Employee Profile), and open another form (Employee Training Records) where it will pull up records pertaining to the current employee profile.
In my where condition currently I have:
="[st_no]=" & [st_no] & " AND [emp_id]='" & [emp_id] & "'"
I get a syntax error saying:
(missing operator) in query expression '[st_no]=IEC 62841-2-5 AND [emp_id]='3"
What am I missing?
Since the field st_no looks to be a string, you'll need to enclose the corresponding value with single or double quotes; conversely, since emp_id looks to be an integer, you don't need the surrounding quotes.
As such, I would suggest:
="[st_no]='" & [st_no] & "' AND [emp_id]=" & [emp_id]
I am having trouble querying a column in an Oracle view that shows up when I pull the schema. In fact, it appears as column number 2 when I list it out.
The error indicates ORA-00904 invalid identifier, which from what I have read says the column name I am referencing is incorrect, but I have copied the name directly from Oracle Developer, MSAccess, and the datareader.Schema, all of which appear to have no issues getting to that column.
If I query the column just using a linked table in MSAccess the data also comes right up. All of the examples I have seen referencing a similar issue in which the field is incorrectly typed, which though I acknowledge is still a possibility, seems unlikely in this case given the direct copy from the column list as mentioned.
Other solutions mention putting the name in double quotes, which I am uncertain how to do in VB.NET or if it is even necessary.
Code below:
'Open And Query
oledbCon.ConnectionString = strCon
oledbCon.Open()
oledbCom.Connection = oledbCon
oledbCom.CommandType = CommandType.Text
oledbCom.CommandText = "SELECT AREA_CODE FROM CSITAPPS.DAYSIN_1057"
oledbda.SelectCommand = oledbCom
oledbda.Fill(gdt)
I was able to find a solution working with a co-worker for a couple days. The issue stems from the fact that Oracle Column references are Case Sensitive. Because of this the double quotations were required, which is tricky for VB.net given quotation marks indicate and encapsulate String entries. The solution was to break the string and concatenate chr(34) into it. That in combination with ensuring that the column reference case matched what was in the table it came right up.
"SELECT " & Chr(34) & "Area_Code" & Chr(34) & " FROM CSITAPPS.DAYSIN_1057 ORDER BY " & Chr(34) & "Area_Code" & Chr(34) & " DESC;"
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!
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
I've read through a couple similar posts, but not found a solution for this issue:
I have a form with an unbound rich text, multiline textbox named tbxNote. When the textbox is exited, I use VBA code to create an SQL string which I subsequently execute to UPDATE a table field [Note] with the value in the unbound textbox. [Note] is a "Long Text" field (from my understanding, "Long Text" is equivalent to what used to be called a "Memo" field). The backend is an Access database.
Problem is: Only the first 250 characters of what is in tbxNote get stored in the target table field [Note] even though other "Long Text" fields in other tables are accepting values much longer than 250 characters. So, it does not seem to be an issue with the field type or characteristics in the backend table.
Furthermore, if I manually open the target table and paste 350 characters into the same [Note] field in the target table, all 350 characters get stored. But, if I load up that record into the form or put the same 350 characters into the form's tbxNote textbox, only 250 characters are pulled into tbxNote or saved out to [Note].
Is there a way to store more than 250 characters in an unbound textbox using an UPDATE SQL in code?
In case it matters, here's the SQL code that I used to prove only 250 of 350 characters gets saved to the table field [Note]:
dbs.Execute "UPDATE tblSupeGenNotes " & _
"SET [NoteDate] = #" & Me.tbxNoteDate & "#, " & _
"[SupeType] = " & Chr(34) & Me.cbxSupeType & Chr(34) & ", " & _
"[SupeAlerts] = " & alrt & ", " & _
"[Note] = " & Chr(34) & String(350, "a") & Chr(34) & " " & _
"WHERE [SupeGenNoteID] = " & Me.tbxSupeGenNoteID & ";"
Of course, normally I'd have Me.tbxNote instead of String(350, "a") but the String proves that only 250 of the 350 characters get stored in the [Note] field.
I must be missing something simple, but I cannot figure it out.
Unfortunately, you posted test code works, but you FAILED to post your actual update string that fails. A common (and known) problem is if you include a function (especially aggregates) in your SQL, then you are limited to 255 characters.
In fact this can apply if you have function(s) that surrounds the unbound text box and is used in the query.
So such an update should and can work, but introduction functions into this mix can cause problems with the query processor.
If you included the actual update, then the above issue(s) likely could have been determined.
So the workarounds are:
Don’t use any “functions” directly in the SQL update string, but build up the string.
So in place of say:
Dbs.Execute "update tblTest set Notes = string(350,’a’)"
Note how above the string function is INSIDE the sql.
You can thus place the function(s) OUTSIDE of the query and thus pre-build the string - the query processor is NOT executing nor will it even see such functions.
So we can change above to as PER YOUR EXAMPLE:
Eg:
Dbs.Execute "update tblTest set Notes = ‘" & string(350,’a’) & "’"
(this is how/why your posted example works, but likely why your actual code fails). So functions can (and should) be moved out of the actual query string.
Also make sure there is NO FORMAT in the formatting for the text box, as once again this will truncate the text box to 255.
And as noted here the other suggestion is to consider using a recordset update in place of the SQL update.
Using a recordset can often remove issues of delimiters and functions then become a non issue.
So such SQL updates can work beyond 255 characters, but functions need to be evaluated in your VBA code before the query processor gets its hands on the data as per above examples.
And as noted remove any “format” you have for the text box (property sheet, format tab).
#HansUp's suggested trying a DAO recordset to update the table. That did the trick! Thank you, HansUp. HansUp requested that I post the answer, so, here is the code that worked for anyone else who comes across this thread:
Dim dbs As DAO.Database
Dim rsTable As DAO.Recordset
Dim rsQuery As DAO.Recordset
Set dbs = CurrentDb
'Open a table-type Recordset
Set rsTable = dbs.OpenRecordset("tblSupeGenNotes", dbOpenDynaset)
'Open a dynaset-type Recordset using a saved query
Set rsQuery = dbs.OpenRecordset("qrySupeGenNotes", dbOpenDynaset)
'update the values vased on the contents of the form controls
rsQuery.Edit
rsQuery![NoteDate] = Me.tbxNoteDate
rsQuery![SupeType] = Me.cbxSupeType
rsQuery![SupeAlerts] = alrt
rsQuery![Note] = Me.tbxNote
rsQuery.Update
'clean up
rsQuery.Close
rsTable.Close
Set rsQuery = Nothing
Set rsTable = Nothing
AH! Another bit to the solution is that prior to using the DAO recordset, I was pulling values from the table into a listbox and from the listbox into the form controls (instead of directly into the form controls from the table). Part of the problem (I believe) was that I was then populating the form controls from the selected item in the listbox instead of directly from the table. I believe listboxes will only allow 255 characters (250 characters?) in any single column, so, everytime I pulled the value into the textbox from the listbox, the code was pulling only the first 255 characters into the textbox. Then, when the textbox was exited, the code was updating the table with the full textbox text, but when it was pulled back into the form through the listbox, we'd be back down to 255 characters. Of course, when I switched to the DAO approach, I also switched to reading the textbox value directly from the table instead of pulling it from the listbox.
Moral: Beware of pulling Long Text values through a listbox!
Thanks to everyone who helped me solve this. Sorry for such a newbie error seeming more complicated than it was.
I assume you are using the SqlClient library. In which case, I recommend trying SqlParameters rather than creating a SQL string the way you are. With the SqlParameter you can specify the size of each parameter. http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2 . I am a C# dev so my apologies about doing the example code below in C#:
string param = "Hello World";
byte [] encodedStr = Encoding.UTF8.GetBytes(param);
SqlParameter sqlParam = new SqlParameter();
sqlParam.Size = encodedStr.Count; // uses byte count
you could condense it by calling Encoding.UTF8.GetBytes(param).Count. Anyways, this might fix your issue