473,668 Members | 2,552 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is primary key value null?

beldom
10 New Member
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?
Jul 21 '14 #1
16 1735
Seth Schrock
2,965 Recognized Expert Specialist
Are you working with related tables at the same time? Do you have any code or macros involved that would be modifying the data?
Jul 22 '14 #2
beldom
10 New Member
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.
Jul 22 '14 #3
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.
Jul 22 '14 #4
beldom
10 New Member
I don't know what you mean by settings of my table.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdSearch_Click()
  2.     On Error Resume Next
  3.     Dim ctl As Control
  4.     Dim sSQL As String
  5.     Dim sWhereClause As String
  6. Dim db As DAO.Database
  7. Dim rs As DAO.Recordset
  8. 'Dim strSQL As String
  9. Set db = CurrentDb
  10.     'Initialize the Where Clause variable.
  11.     sWhereClause = " Where "
  12.     'Start the first part of the select statement.
  13.     sSQL = "select * from Patients "
  14.     'sSQL = "INSERT INTO Patients (MedRec,Gender) VALUES (1121, 'R');"
  15.  
  16.     'Loop through each control on the form to get its value.
  17.     For Each ctl In Me.Controls
  18.         With ctl
  19.             'The only Control you are using is the text box. However, you can add as many types of controls as you want.
  20.             Select Case .ControlType
  21.                 Case acTextBox
  22.                     .SetFocus
  23.                     'This is the function that actually builds the clause.
  24.                     If sWhereClause = " Where " Then
  25.                         sWhereClause = sWhereClause & BuildCriteria(.Name, dbText, .Text)
  26.                     Else
  27.                         sWhereClause = sWhereClause & " and " & BuildCriteria(.Name, dbText, .Text)
  28.                     End If
  29.             End Select
  30.         End With
  31.     Next ctl
  32.     'Set the forms recordsource equal to the new select statement.
  33.     Me.txtSQL = sSQL & sWhereClause & ";"
  34.     'Me.RecordSource = sSQL & sWhereClause & ";"    'Me.Query = sSQL & sWhereClause   'Me.Requery
  35.     'db.Execute sSQL & sWhereClause & ";", dbFailOnError
  36.  
  37. 'Me.List149.RowSource = db.OpenRecordset("select * from Patients;")
  38. Set rs = db.OpenRecordset(sSQL & sWhereClause & ";")
  39.  
  40.     'Do While Not rs.EOF
  41.     '    Me.List149.AddItem (rs![First Name] & ";" & rs![Last Name])
  42.     '    rs.MoveNext
  43.     'Loop
  44.  
  45.                 'Dim db As DAO.Database
  46.                 'Dim rs As DAO.Recordset
  47.                 Dim lst As ListBox
  48.                 'strBuild = ""
  49.  
  50.                 Set lst = Me![List149]
  51.  
  52.                 'Set up the Listbox correctly, if it isn't already
  53.                 lst.RowSourceType = "Value List"
  54.                 lst.ColumnCount = 3     '##################################################################
  55.                 lst.BoundColumn = 3     '##################################################################
  56.                 lst.ColumnWidths = "1 in;1 in;1 in" '##################################################################
  57.  
  58.                 'Set db = CurrentDb
  59.  
  60.                 'Set rs = db.OpenRecordset("SELECT * FROM AnswerT WHERE QuestionID =" & QuestionID)
  61.  
  62.                 Do While Not rs.EOF
  63.                     '##################################################################
  64.                     strBuild = strBuild & rs!Gender & ";" & rs![Last Name] & ";" & rs![First Name] & ";"
  65.                      rs.MoveNext
  66.                 Loop
  67.  
  68.                 lst.RowSource = Left$(strBuild, Len(strBuild) - 1)
  69.  
  70.  
  71.  
  72.  
  73.  
  74. rs.Close
  75. Set rs = Nothing
  76.     Set db = Nothing
  77.     Me.Requery
  78.     Me.Refresh
  79.     End Sub
  80.  
  81.  
Jul 22 '14 #5
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?
Jul 22 '14 #6
beldom
10 New Member
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?
Jul 22 '14 #7
beldom
10 New Member
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.
Jul 22 '14 #8
twinnyfo
3,653 Recognized Expert Moderator Specialist
Concerning Post #7, to prevent errors, when you use a blank control, you can use a condition:

Expand|Select|Wrap|Line Numbers
  1. If Not IsNull(Me.ControlName) Then...
That should prevent certain problems...
Jul 22 '14 #9
beldom
10 New Member
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull(Me.Controls) Then
  2.                     strBuild = strBuild & "" & ";" & "" & ";" & "" & ";"
  3.                      rs.MoveNext
  4.                      End If
This clears the listbox, but also, this kills the database (crashes). What's wrong here?
Jul 22 '14 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2471
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
1
3144
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...
3
7507
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.
12
4522
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...
7
1682
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
0
2077
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...
2
2189
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>
9
1976
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...
4
5596
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')
1
2152
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()
0
8459
marktang
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...
0
8367
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,...
0
8889
Oralloy
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...
0
7391
agi2029
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...
0
5677
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();...
0
4202
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...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2781
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
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.