0
votes

I have tried to search this but found nothing that helped me. I am trying to work on a school project where I need to add a record to an Access database but I am getting the error "dataset not in edit or insert mode.

sName := ledName.Text;
sUName := ledUserName.Text;
sPass := ledPassword.Text;
sEmail := ledEmail.Text;
sCell := ledPhone.Text;

dmUsers.tblUsers.ReadOnly := False;

dmUsers.tblUsers.Open;
dmUsers.tblUsers.Insert;

dmUsers.tblUsers.Last;
dmUsers.tblUsers['UserName'] := sUName;
dmUsers.tblUsers['Password'] := sPass;
dmUsers.tblUsers['Email'] := sEmail;
dmUsers.tblUsers['Cell Number'] := sCell;
dmUsers.tblUsers['Actual Name'] := sName;
dmUsers.tblUsers['Balance'] := 0;
dmUsers.tblUsers['Points'] := 0;

dmUsers.tblUsers['Present'] := False;

dmUsers.tblUsers.Post;

I don't know what I'm doing wrong. Any help would be appreciated.

1
Call Edit before editing dataset. Then just call Append (or Insert as you did) and remove your Last call. - Victoria

1 Answers

2
votes

If you call methods on the dataset that can change the active record (like First, Last, Next or Locate) the dataset will call CheckBrowseMode, which will Post changes if modifed or Cancel the fresh record if not modifed. The result is the dataset being in dsBrowse, causing it to not accept editing.

So change your code to something like the following. I'll use Append, as your Last call suggests you wanted to have the new record at the end of the dataset.

sName := ledName.Text;
sUName := ledUserName.Text;
sPass := ledPassword.Text;
sEmail := ledEmail.Text;
sCell := ledPhone.Text;

dmUsers.tblUsers.ReadOnly := False;

dmUsers.tblUsers.Open;

dmUsers.tblUsers.Append;

dmUsers.tblUsers['UserName'] := sUName;
dmUsers.tblUsers['Password'] := sPass;
dmUsers.tblUsers['Email'] := sEmail;
dmUsers.tblUsers['Cell Number'] := sCell;
dmUsers.tblUsers['Actual Name'] := sName;
dmUsers.tblUsers['Balance'] := 0;
dmUsers.tblUsers['Points'] := 0;

dmUsers.tblUsers['Present'] := False;

dmUsers.tblUsers.Post;