I'm having the error message "Index or primary key cannot contain null value" pop up even though I haven't updated or otherwise changed the primary key. Why could this be?
16 1735
Are you working with related tables at the same time? Do you have any code or macros involved that would be modifying the data?
Nope. The only thing the form does is search (select) items from the table. Tomorrow I will try two things: 1) Delete the table and start a new one to make sure that there are no settings or other criteria that make an empty cell in the primary key field 2) Delete the code in the button and close the form, because that is when the error message appears. Will let you know then if either solution works.
twinnyfo 3,653
Recognized Expert Moderator Specialist
If your primary key is not an autonumber, you may be moving to a new record? If you post the settings of your table and the code in your Form, perhaps we could troubleshoot better.
I don't know what you mean by settings of my table. - Private Sub cmdSearch_Click()
-
On Error Resume Next
-
Dim ctl As Control
-
Dim sSQL As String
-
Dim sWhereClause As String
-
Dim db As DAO.Database
-
Dim rs As DAO.Recordset
-
'Dim strSQL As String
-
Set db = CurrentDb
-
'Initialize the Where Clause variable.
-
sWhereClause = " Where "
-
'Start the first part of the select statement.
-
sSQL = "select * from Patients "
-
'sSQL = "INSERT INTO Patients (MedRec,Gender) VALUES (1121, 'R');"
-
-
'Loop through each control on the form to get its value.
-
For Each ctl In Me.Controls
-
With ctl
-
'The only Control you are using is the text box. However, you can add as many types of controls as you want.
-
Select Case .ControlType
-
Case acTextBox
-
.SetFocus
-
'This is the function that actually builds the clause.
-
If sWhereClause = " Where " Then
-
sWhereClause = sWhereClause & BuildCriteria(.Name, dbText, .Text)
-
Else
-
sWhereClause = sWhereClause & " and " & BuildCriteria(.Name, dbText, .Text)
-
End If
-
End Select
-
End With
-
Next ctl
-
'Set the forms recordsource equal to the new select statement.
-
Me.txtSQL = sSQL & sWhereClause & ";"
-
'Me.RecordSource = sSQL & sWhereClause & ";" 'Me.Query = sSQL & sWhereClause 'Me.Requery
-
'db.Execute sSQL & sWhereClause & ";", dbFailOnError
-
-
'Me.List149.RowSource = db.OpenRecordset("select * from Patients;")
-
Set rs = db.OpenRecordset(sSQL & sWhereClause & ";")
-
-
'Do While Not rs.EOF
-
' Me.List149.AddItem (rs![First Name] & ";" & rs![Last Name])
-
' rs.MoveNext
-
'Loop
-
-
'Dim db As DAO.Database
-
'Dim rs As DAO.Recordset
-
Dim lst As ListBox
-
'strBuild = ""
-
-
Set lst = Me![List149]
-
-
'Set up the Listbox correctly, if it isn't already
-
lst.RowSourceType = "Value List"
-
lst.ColumnCount = 3 '##################################################################
-
lst.BoundColumn = 3 '##################################################################
-
lst.ColumnWidths = "1 in;1 in;1 in" '##################################################################
-
-
'Set db = CurrentDb
-
-
'Set rs = db.OpenRecordset("SELECT * FROM AnswerT WHERE QuestionID =" & QuestionID)
-
-
Do While Not rs.EOF
-
'##################################################################
-
strBuild = strBuild & rs!Gender & ";" & rs![Last Name] & ";" & rs![First Name] & ";"
-
rs.MoveNext
-
Loop
-
-
lst.RowSource = Left$(strBuild, Len(strBuild) - 1)
-
-
-
-
-
-
rs.Close
-
Set rs = Nothing
-
Set db = Nothing
-
Me.Requery
-
Me.Refresh
-
End Sub
-
-
twinnyfo 3,653
Recognized Expert Moderator Specialist
Is your primary key an autonumber, or do you set the value in another way?
And, which line of the code you posted is generating the error?
Anyway, I created a new table (called "Patients") and it seems to have fixed the issue.
Anyone know how I can make it so when I input a blank search value the result shows a blank listbox instead of causing Access to freeze and have to be restarted?
It wasn't the code, it was the table. It wasn't an autonumber but it is now, which seems to have been what was wrong...Perhaps when inputting data into the table I forgot to input a primary key (which was the Medical Record # for patients) and even though I later appended the missing field, Access held a grudge. So it would seem.
twinnyfo 3,653
Recognized Expert Moderator Specialist
Concerning Post #7, to prevent errors, when you use a blank control, you can use a condition: - If Not IsNull(Me.ControlName) Then...
That should prevent certain problems...
- If Not IsNull(Me.Controls) Then
-
strBuild = strBuild & "" & ";" & "" & ";" & "" & ";"
-
rs.MoveNext
-
End If
This clears the listbox, but also, this kills the database (crashes). What's wrong here?
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Ken |
last post by:
I wrote a function to use in queries that takes a date and adds or
subtracts a certain length time and then returns the new value. There
are times when my function needs to return Null values.
Function DateCalc (blah...) As Variant
Do Stuff...
If Not IsNull(varNewDate) Then
DateCalc = varNewDate
End If
End Function
|
by: J |
last post by:
Situation - Using the table pubs.titleauthor in a strongly typed dataset,
attached to a datagrid, using the datagrid I would like to set the
royaltyper field = null (the database allows nulls in this field). But when
click on the cell and remove the field value (leaving a blank), and move to
the next record, the blank cell is filled with the original data.
When I view the schema (.xsd file) of the strongly typed dataset, and try to...
|
by: Fabio Negri Cicotti [MCP] |
last post by:
Hi All.
I'm trying to insert data into 2 tables (parent-child) using the ADO.NET's
SetParentRow method. The parent table has an identity column as primary key.
When I execute the code below I get the following message: "Cannot insert
the value NULL into column 'hdtID', table
'myinstance.mydb.Hotel_Details_Lang'; column does not allow nulls. INSERT
fails." Any idea?
PS: To create the DataAdapters, I'm using SqlCommandBuilder.
|
by: Aidan |
last post by:
I have a form to create a new record for a training course. The form is based on one table that has 4 keys set to primary key.
The first combo box on the form allows selection of the course POP code and this then fills details on 2 list boxs for course module code and description. The course section can then be selected from another combo box (filtered by the value in the first combo box) . Finally the course session name is entered...
|
by: Sala |
last post by:
Hi experts!
If system time 12:00 am my application variable value will be
null.. I am trying to get null value of application but that time i'll
make some actions to that page.. So pls tell me how i'll make null
| |
by: diane |
last post by:
Just trying to upsize using VFP 9 with SQL 2005 using VFP remote
views. One in particular keeps coming up and saying Cannot insert the
value NULL into column, when I really don't think I am inserting a
NULL value. Oddly sometimes it works but I don't know why. Can anyone
suggest anything else I can try? Perhaps this is the wrong group I'm
posting to - it was just the one that came up with other errors giving
the same message, but none of...
|
by: artev |
last post by:
if I insert a string null in a select,
it change position; why?
I insert value "" in 2nd 3th select;
<style type="text/css">
td {border:2px solid pink;};
</style>
|
by: ramdil |
last post by:
Hi All
I have a list box in a form with three columns
1.ID
2.Name
3.Age
In the same form i have three text boxes corresponding for the above columns.
The form record source property is set to the Table.ID field is autonumber.
Now i am facing a error.When i select one row in a list box ,i want to assign the values in the list box row back to text box for updation purpose.Now Name and age i am able to assign but for ID it is showing the...
|
by: qwedster |
last post by:
Howdy folks!
I am using stored procedure to see if a value exists in database table and return 0 if exists or else -1, in the following SQL queries.
However how to check if a value (that is null) exists in database table and return 0 if exists or else -1 using stored procedure? Please help.
USE
GO
IF EXISTS (SELECT name FROM sys.databases WHERE name = 'ExampleDatabase')
|
by: Octo Siburian |
last post by:
I have been collecting data from ms.access database into a class object
'_Get and put in data from database Fingerprint(RAS) into CPresensiFingerprint
Public Function GetdbFingerprint() As CPresensiFingerprint
Dim presensiFinger As New CPresensiFingerprint
Dim SQLSelectdbRAS As String
Dim rsFinger As Recordset
Dim dateNow_ As Date
dateNow_ = um_TgldanJamSkrg()
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |