Adding new record with acNewRec and dbs.Execute - vba

I'm stuck on something in MS Access VBA.
I'm trying to add a new record to the table and also adding extra field to a different table.
This is part of the vba code I'm using.
'Save the match to the table
DoCmd.GoToRecord , , acNewRec
'Step from 1 to the ResultValue
For LCounter = 1 To ResultHomeTeam.Value
Select Case LCounter
Case 1
dbs.Execute " INSERT INTO tblMatchPlayer " _
& "(MatchID, PlayerID, SubstituteID, PositionID, Surname, ScoreTime, RedCards, YellowCards, Substitude, Penalty, OwnGoal, Assist) VALUES " _
& "(" & Me.MatchID & ", '', '', '', '" & Me.cmScoreName1 & "', " & Me.tbScoreTime1 & ", '', '', '', " & Me.cbPenalty1 & ", " & Me.cbOwnGoal1 & ", '" & Me.cmAssist1 & "');"
However this isn't working, if I only use the dbs.Execute it is writing the values to the correct table but then it isn't writing the Match details to the Match Table
I've got it like this
1st part of the form is match details
Date
Opponent
ScoreHomeTeam
ScoreAwayTEam
That should be saved by acNewRec
And the second part of the form are player details who scored
PlayerName
ScoreTime
OwnGoal
Assist
That has to be saved by dbs.execute
No I got two buttons one, to save the match details on it's own and one to save the player details.
But I would like to have one button to save all of it, but when I try to combine acNewRec and dbs.Execute it isn't working.
The problem is I have to save the match details first before I can save the player details cause player details has to have MatchID.
Can anybody help me?
With kind regards,
Patrick

The problem part was the first save of the form data.
To save the current record (and stay on it), use DoCmd.RunCommand acCmdSaveRecord, not DoCmd.GoToRecord , , acNewRec.
The latter does save, but goes to a new, empty record on the form, so you can't use the previously entered values.

Related

MS-Access updating first record when adding record

I'm fairly new to access but not to vba. I have a form that inserts data to a table with vba
rcrdAdd = "INSERT INTO " & timeTbl & " (ProjectNumber, AssignedTo, Task, taskStart) VALUES ('" & ProjectNumber.Value & "', '" & combobox.value & "', '" & combobox.value & "', '" & datetime.value & "');"
db.Execute rcrdAdd, dbFailOnError
When i run this it does add the record but it also updates the first record in the table with the project number associated with record it just added.
I don't know what or why. Can anyone shed any light here. I have tried to research why and can not find an answer
Your form is bound to a the table's recordsource and it looks like you've created a project number text box that is bound to the field
Then it looks like you're typing in the new project number (ProjectNumber.Value) for the Insert.
That will automatically update the current record when you leave - most likely you're opening the form to the first record in the table
Just create a new TEXTBOX that is UNBOUND - and use that in your INSERT INTO SQL instead of ProjectNumber.Value

Searching a table where the field doesnt exist - Access VBA

I am a data consultant who migrates data I am sent into our system. I have written code that compares the contents of my table against what has been put into oracle, as an extra test. The tables are a little convoluted due to how they relate to each other. But essentially here is my question:
When I look to match two field values and the field doesnt exist I get a parameter pop up box. I want to only run the code if the field exists.
I have tried many things, wrapping an if statement around it but I always get the parameter box, can anyone help there must be an easier way to do this!
If Not DoCmd.OpenQuery("SELECT TOP 1" & MatchValues!FieldName & " FROM " &
MatchValues!ORACLE_TABLE_NAME) Then
MsgBox "moomins"
' strSQL = "INSERT INTO 002_TableValueErrors(ORACLE_TABLE_NAME,TRANSFORM_TABLE_NAME,FIELD_NAME,ORACLE_FIELD_VALUE,TRANSFORM_FIELD_VALUE) "
' strSQL = strSQL & " VALUES (" & MatchValues!ORACLE_TABLE_NAME & "," & MatchValues!TRANSFORM_TABLE_NAME & "," & MatchValues!FieldName & ",'ORACLE: NOT FOUND','ORACLE: NOT FOUND')"
End If
If you deal with Oracle: Have you tried to check if the field exists by querying ALL_TAB_COLUMNS in Oracle?
"Select count(*) from ALL_TAB_COLUMNS where table_name = " & MatchValues!ORACLE_TABLE_NAME & " and COLUMN_NAME = " & MatchValues!FieldName
(Untestet cause currently I have no Oracle Instance available)

MS Access VBA, not getting results when running DoCmd to find a record

I am not a programmer by any means but I am trying to get a small data collection database going. I need to find a record based on input. Ive got two criteria that I want it to find, and if both arent found together, it is supposed to create a new record (with both fields filled out to prevent duplication of the pair). First criteria is that it needs to look for a date. I figured that one out already, here is the code.
Dim Date1 As String
Date1 = Text6.Value
DoCmd.OpenForm "Data", , , "Data![ShiftDate] = #" & Date1 & "#"
Works like a charm. Now, I tried to add the second criteria in. The second criteria pulls from a listbox, either Day or Night. Then, it is supposed to search my data fields for that same criteria in a list box on my Data! form.
Dim Date1 As String
Dim Shift1 As String
Date1 = Text6.Value
Shift1 = List12.Column(1)
MsgBox Shift1
DoCmd.OpenForm "Data", , , _
"Data![ShiftDate] = #" & Date1 & "# AND Data![Shift] ='" & Shift1 & "'"
the msgbox was just to verify that it is pulling the right words, day or night. Everything works great, but instead of pulling a record on the right date, and with the right shift, I get blank records. I tried taking out the first half of the where statement to isolate the shift part and still just cannot get it to find a record. I have no idea what I am doing wrong.
It seems that you already have a form.
I recommend you trying this:
Add a button with a click event to the form and add following code to the click event:
dim rs as dao.recordset
set rs = currentdb.openrecordset ("SELECT * FROM [yourtable] WHERE ('ShiftDate' = '" & [yourParameter] & "' AND 'Shift' = '" & [your2ndParameter] & "')")
if rs.eof then
' here NO record is found
currentdb.execute ("INSERT INTO [yourtable] ('ShiftDate', 'Shift') VALUES ('" & [yourParameter] & "', '" & [your2ndParameter] & "')")
else
' here a record is found
endif
Since your ShiftDate is a date you need to convert your date to a SQL date Format, like this:
Function SQLDatum(Datumx) As String
If IsDate(Datumx) Then
SQLDatum = Format(CDate(Datumx), "\#yyyy-mm-dd\#", vbMonday, _
vbFirstFourDays)
Else
SQLDatum = ""
End If
End Function
In your command
DoCmd.OpenForm "Data", , , "Data![ShiftDate] = #" & Date1 & "# AND ...
You open a form Data but at the same time you specify a filter by referring to the form which is being opened with Data![ShiftDate]. This is very strange, since the filter must be applied to the data source. I would simply write
DoCmd.OpenForm "Data", , , "ShiftDate= #" & Date1 & "# AND Shift= '" & Shift1 & "'"
by referring to the table columns of the data source (on the left side of =).
Note that the column Shift must be a text column. If it was some number, you would have to drop the single quotes and pass it a corresponding id.

How to check if record is existing in ms access database using vb.net?

I have to publish vacant positions in publication system. But before saving in database it will check the Position title in "Postit" column/field and item number in "itemno" column/field if the text already existing in record in the same row. I dont know if it is correct to put many "WHERE" inside the .Open clause or can I put "AND" to avoid many "WHERE". Can you please help me solve this. Thanks... Some part of the codes is stated below:
Error in this line of code:
.Open("Select * from pop where vacantnotvacant ='" & "vacant" & "' , where Position = '" & ListViewEx1.Items.Item(h).SubItems(2).Text & "'", psipopcon, 2, 3)
Try this in place of your open line
.Open("Select * from pop where vacantnotvacant ='" & "vacant" & "' and Position = '" & ListViewEx1.Items.Item(h).SubItems(2).Text & "'", psipopcon, 2, 3)

Filtering data for any character that begins with what the user input using vba access

Hello everyone I am trying to filter data in a table using vba where I have a split form setup. My goal is to filter data where the user can input a character such as "a" for the "FirstName" field and have results from the table filtered with persons first name that starts with "a". I have modified the code that I had setup with the help of a fellow member for comparing exact values to incorporate any value. Here is my code so far for one of the text boxes.
Private Sub txtFirstName_DblClick(Cancel As Integer)
If Me.Filter = "" Then
'Compares the values that begin with the input values in txtFirstName
'text box from the table field name FirstName
Me.Filter = FirstName & " LIKE '" & txtFirstName & "*'"
Else
'this line is suppose to append all the filtered data that were acquired
Me.Filter = Me.Filter & AND LastName & " LIKE '" & txtLastName & "*'"
End If
FilterOn = True
End Sub
I get an error in the else statement and please note that I am linking this form to an SQL server so I can not delete or modify existing data in the table I am a novice at this so I am still learning. I have seen similar post to questions like this but I am looking for a specific post related to this.
You are missing some quote symbols.
The If block should be:
Me.Filter = "FirstName LIKE '" & txtFirstName & "*'"
The else block should be:
Me.Filter = Me.Filter & " AND LastName LIKE '" & txtLastName & "*'"