473,386 Members | 1,702 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to insert text into a new field based on the positioning of a string?

Hi all,

I am required to write a code to check the positioning of a text in a string.

E.g.

"~[Admin]~xxxxxxxxx~ACK~" = Acknowledged
"~xxxxxxxxx~[Admin]~ACK~" = Not Acknowledged
"~xxxxxxxxx~ACK~[Admin]~" = Not Acknowledged

As you can see, as long as the string starts with "~[Admin]~", it is counted as acknowledged. If the "~[Admin]~" appears anywhere else except the start, it is counted as not acknowledged.

I want to make it such that when the code runs, the second column of the table states whether the event was acknowledged or not. I've attached screenshots of what it should look like.

Before:



After:



My current code is like super buggy and does not even come close to getting the job done. It skips the first 3 records and only starts inserting at the fourth.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Command2_Click()
  2.     Dim IsOK As Boolean
  3.     Dim dbs As DAO.Database
  4.     Dim rst As DAO.Recordset
  5.     Dim rstInsert As DAO.Recordset
  6.  
  7.     Set dbs = CurrentDb
  8.     Set rst = dbs.OpenRecordset("Table5")
  9.     Set rstInsert = dbs.OpenRecordset("Table5")
  10.  
  11.     If Not rst.EOF Then
  12.         Do
  13.             rst.AddNew
  14.             If InStr(rst![Field1], "~[Admin]~") = 1 Then
  15.                 IsOK = True
  16.                 rstInsert![Field2] = "yes"
  17.             Else
  18.                 IsOK = False
  19.                 rstInsert![Field2] = "no"
  20.                 rstInsert.Update
  21.             End If
  22.             rst.MoveNext
  23.         Loop Until rst.EOF
  24.     End If
  25.  
  26. End Sub
Attached Images
File Type: jpg Capture.JPG (18.8 KB, 202 views)
File Type: jpg Capture2.JPG (19.2 KB, 174 views)
Nov 25 '14 #1
3 1077
Ok, new plan. I'm thinking of extracting both the string and the "yes" or "no" into another table.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command8_Click()
  2.     Dim dbs As DAO.Database
  3.     Dim rst As DAO.Recordset
  4.     Dim rstInsert As DAO.Recordset
  5.  
  6.     Set dbs = CurrentDb
  7.     Set rst = dbs.OpenRecordset("Table2", dbOpenDynaset)
  8.     Set rstInsert = dbs.OpenRecordset("Acknowledgement", dbOpenDynaset)
  9.  
  10.     If Not rst.EOF Then
  11.         Do
  12.             rstInsert.AddNew
  13.             If InStr(rst![Field1], "~[ admin]~") = 1 Then
  14.                 rstInsert![Field1] = rst![Field3]
  15.                 rstInsert![Field2] = "Yes"
  16.             Else
  17.                 rstInsert![Field1] = rst![Field3]
  18.                 rstInsert![Field2] = "No"
  19.             End If
  20.             rstInsert.Update
  21.             rst.MoveNext
  22.         Loop Until rst.EOF
  23.     End If
  24.  
  25.     rst.Close
  26.     Set rst = Nothing
  27.     rstInsert.Close
  28.     Set rstInsert = Nothing
  29.  
  30. End Sub
This code managed to do it, but all the acknowledgements are marked as "no" even though some are supposed to be "yes".

PS, "~[ admin]~" is for my actual data. "~[Admin]~" is for example purposes
Nov 25 '14 #2
Ok, never mind! I found my problem! I forgot to change the name of the field in the If statement
Nov 25 '14 #3
Luuk
1,047 Expert 1GB
Would an update query not be simpler?

Expand|Select|Wrap|Line Numbers
  1. UPDATE Table5 
  2. SET Table5.[Field2] = IIF(InStr(rst![Field1], "~[ admin]~") = 1, "yes", "no")
  3.  
Nov 25 '14 #4

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

Similar topics

1
by: John Davis | last post by:
I want to display credit card fields when the user selects the option paid membership. If user selects the option free membership, then the credit card text field will disappear. Here's the code I...
13
by: pkinville | last post by:
ok, this is gonna seem stupid, but it has been stumping me all afternoon. I have two fields on a HTML page. HTML looks like this... <html> <head> <title>New Page 3</title> </head> <body>
1
by: Neha Jain | last post by:
i have a table 'Tab1' with some fields. i want to create a screen consisting one of the fields in Table 'Tab1'.user must be able to select any of the data stored in the table for that field and any...
4
by: Ruben | last post by:
Hi, I have a continuous form that provides a listing of various instruments that are serviced on a monthly to annual basis, with general info about the instrument, last and next service dates,...
10
by: dkyadav80 | last post by:
<html> /// here what shoud be java script for: ->when script run then not display all input text field only display selection field. ->when user select other value for institute only this...
2
by: mckurban | last post by:
Hi All, I'm not very familiar with Javascript and need help with setting up some javascript code to allow users to create dynamic radio buttons from text field and then to use selected radio value...
11
by: DBlearner | last post by:
Hello folks, I'm building a nutrition database to keep track of my eating habit. I know there's ton out there but I want to do it on my own you know? Anyway there's a tiny issue that I cant...
6
Seth Schrock
by: Seth Schrock | last post by:
I found a function online that finds the cursor in a textbox, and then allows me to insert a string where the cursor is. When I call it as the website suggested, it works fine. However, it selects...
1
by: grandmajulie11 | last post by:
I have a form that has a field called Name. I want the background to change from white to pink if the Gender field says Female and Blue if the Gender says Male and to remain white if blank. I am...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.