How to select all tables excluding tables that have a border? - vba

I have a document that contains multiple tables. I want to select all tables excluding tables that have a border.
Found this online:
Sub selecttables()
Dim mytable As Table
For Each mytable In ActiveDocument.Tables
mytable.Range.Editors.Add wdEditorEveryone
Next
ActiveDocument.SelectAllEditableRanges (wdEditorEveryone)
ActiveDocument.DeleteAllEditableRanges (wdEditorEveryone)
End Sub
It selects all tables in the document.

Related

How to delete rows from both tables linked to each table by their primary keys?

I want to delete the rows from users and hobbies where they are connected by ID and customersID respectively.
I am using Microsoft access to do that. I linked the 2 primary keys from both table.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim str As String = IDbox.Text
If String.IsNullOrEmpty(str) Then
MsgBox("Error") : Exit Sub
End If
Access.AddParam("#ID", str)
Access.ExecQuery("DELETE FROM users where ID = #ID")
If Not String.IsNullOrEmpty(Access.Exception) Then MsgBox(Access.Exception) : Exit Sub
update_Table()
update_TableHobby()
End Sub
How can I delete rows in both table?
Like currently if I execute the query it will just delete from users table but not from Hobbies table.

Creating a result set of data using data from one table joining on another table containing two key columns

I'm trying to create a summarised result set joining two tables.
The first table (main and multi row) contains say the following columns:
trans_id,
trans_type_id
The second table (one row only) contains:
from_trans_type_id,
to_trans_type_id
I'm trying to join the two tables so that from_trans_type_id = trans_type_id and to_trans_type_id = trans_type_id and get the relevant trans_id values
I've tried self joining and derived joins to no effect.
The end result is that I'm looking to get a result set that looks something like this:
trans_id as from_trans_id, from_trans_type_id, trans_id as to_trans_id, to_trans_type_id
data is:
you can use join with multiple instance of firsttable
select b.trans_id as from_trans_id, from_trans_type_id, c.trans_id as to_trans_id, to_trans_type_id
from secondtable a
inner join firsttable b on a.from_trans_type_id=b.trans_type_id
inner join firsttable c on a.to_trans_type_id=c.trans_type_id

SQL (VBA/ADO) command to populate NULL fields with corresponding non-NULL values from duplicate records

I have a database where several hundred records have been duplicated. However the duplicated information is not the same across all fields. For any two lines, the first line will contain information in some fields while the duplicate line's fields are blank; but then for other fields, the duplicate (second) line will contain information while the first line's fields are blank. For example, it looks like this:
ID Deleted Reference Name Case_Date Outcome Outcome_Date
100 False A123 Chris 2000-01-01 Yes
101 False A123 Chris 2000-03-31
The ID column is a unique primary key for the record.
The Reference column is the one by which I can identify the duplicates.
However as you can see, the first record (100) contains information in Case_Date and Outcome, but the second record (101) contains an Outcome_Date.
What I want to do is to copy the most amount of information into just one of each pair of records, and then mark the duplicate as deleted (I use a soft-delete, not actually removing records from the table but just flagging the Duplicate column as True). With the above example, I want it to look like this:
ID Deleted Reference Name Case_Date Outcome Outcome_Date
100 False A123 Chris 2000-01-01 Yes 2000-03-31
101 True A123 Chris (2000-01-01)* (Yes)* 2000-03-31
Technically it will not be necessary to also copy information into the blank fields of the record which will be marked as deleted, but I figure it's easier to just copy everything and then mark the "second" record as the duplicate, rather than trying to work out which one contains more information and which one contains less.
I am also aware that it will be easier to run a separate SQL command for each column than to try to do them all at once. The columns shown above are a simplified example, and the information which may or may not be present across each column differs.
My select query for the record set of duplicates is:
SELECT *
FROM [Cohorts]
WHERE [Deleted] = False
AND ([CaseType] = "Female" OR [CaseType] = "Family")
AND [Reference] Is Not Null
And [Reference] In (SELECT [Reference] FROM [Cohorts] As Tmp
WHERE [Deleted] = False
AND ([CaseType] = "Female" OR [CaseType]="Family")
GROUP BY [Reference]
HAVING Count(*) > 1)
ORDER BY [Reference];
This will return all (Female/Family) records in the table [Cohorts] where there exists more than one record with the same Reference (and where the records have not been marked as deleted).
I'm running my queries from VBA via ADO, so can execute UPDATE statements. My database is an Access-compatible .mdb using the JET engine.
Grateful if anyone could suggest a suitable SQL command which I can run per column in order to populate the NULL fields with the values of the non-NULL fields from the relevant duplicate records. It's a bit beyond my SQL understanding at present! Thanks.
My first UPDATE JOIN ever, hope it works (untested):
update t1
set t1.name = coalesce(t1.name, t2.name),
t1.Case_Date = coalesce(t1.Case_Date, t2.Case_Date),
t1.Outcome = coalesce(t1.Outcome, t2.Outcome),
t1.Outcome_Date = coalesce(t1.Outcome_Date, t2.Outcome_Date),
t1.deleted = case when t1.id < t2.id then FALSE else TRUE end
from Cohorts t1
join Cohorts t2 on t1.Reference = t2.Reference and t1.id <> t2.id
Edit: Alternative solution:
Create a copy table, do insert select:
insert into CohortsCopy (Deleted, Reference, Name, Case_Date, Outcome, Outcome_Date)
select case when t1.id < t2.id or t2.id is null then FALSE else TRUE end,
coalesce(t1.Reference, t2.Reference),
coalesce(t1.name, t2.name),
coalesce(t1.Case_Date, t2.Case_Date),
coalesce(t1.Outcome, t2.Outcome),
coalesce(t1.Outcome_Date, t2.Outcome_Date)
from Cohorts t1
left join Cohorts t2 on t1.Reference = t2.Reference and t1.id <> t2.id
Then either rename, or copy back to original table.

Foxpro/SQL copy values from cursor to table where id's match

I have a Excel document with a ID column and a Memo type column. (I've made a cursor named Sheet01)
I also have a table named Caption with a ID column, Memo type column and a Code (char).
I need to insert the values of the Memo column from document into the table and assign a existent column a value of my choice (in this case 'DE').
The ID column in the cursor Sheet01 has all numers like this: 28.00000000 (from 1 to ~ 1200)
While the Caption table has like this: 28
This is my best try but obviously it does not work. Help!
INSERT INTO captions2(captionid, caption, code)
SELECT captionid, <caption from the colum 'Header'>, 'DE' FROM Sheet01 WHERE c2.Captionid = Sheet01.Captionid
Sample picture:
http://i.imgur.com/4ugkR7v.png
How do I get the data from excel to cursor? I have a function for that but is similar to APPEND FROM or any other way.
Your SELECT statement isn't correct. Try joining the tables like below.
INSERT INTO captions2(captionid, caption, code)
SELECT s.captionid, c.caption, "DE"
FROM sheet01 s INNER JOIN captions2 c
ON s.captionid = c.captionid

Update table rows with data from second table

I am trying to update specific rows in a master table from data in a secondary table.
I have a master table of 100,000 rows with a number of columns one of which is "UniqueCode", a unique Alpha Code and another is "Data", which contains a string of text . I have a smaller secondary table of about 2,000 rows with 2 columns, "New_Data" and "Code". "New data" is another string and "Code" is Alpha Code which is equal to a "UniqueCode" from the Master Table.
I want to update the column "Data" in the Master table when the Alpha Code from the secondary table is equal to the Alpha Code from the Master table and set it to be the same as "New_data" from the secondary table.
Would appreciate any help with this as struggling to find a way to do this.
UPDATE MasterTable
SET
Data = s.New_Data
FROM MasterTable m
INNER JOIN SecondaryTable s ON m.UniqueCode = s.Code
update master
set data = s.new_data
from master m
inner join second s on m.alphacode = s.code
or
update master
set data = s.new_data
from master m, second s
where m.alphacode = s.code